Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Adding an ignore workspace option #10916

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api/schema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,9 @@ pub const Api = struct {
/// concurrent_scripts
concurrent_scripts: ?u32 = null,

/// ignore_workspace
ignore_workspace: ?bool = null,

pub fn decode(reader: anytype) anyerror!BunInstall {
var this = std.mem.zeroes(BunInstall);

Expand Down
6 changes: 6 additions & 0 deletions src/bunfig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ pub const Bunfig = struct {
}
}
}

if (_bun.get("ignoreWorkspace")) |optional| {
if (optional.asBool()) |ignore_workspace| {
install.ignore_workspace = ignore_workspace;
}
}
}

if (json.get("run")) |run_expr| {
Expand Down
24 changes: 19 additions & 5 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ pub const Arguments = struct {
};

defer config_file.close();

const contents = config_file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch |err| {
if (auto_loaded) return;
Output.prettyErrorln("<r><red>error<r>: {s} reading config \"{s}\"", .{
Expand All @@ -286,6 +287,7 @@ pub const Arguments = struct {
ctx.log.level = original_level;
}
ctx.log.level = logger.Log.Level.warn;

try Bunfig.parse(allocator, logger.Source.initPathString(bun.asByteSlice(config_path), contents), ctx, cmd);
}

Expand All @@ -297,7 +299,18 @@ pub const Arguments = struct {

return null;
}

pub fn loadConfig(allocator: std.mem.Allocator, user_config_path_: ?string, ctx: Command.Context, comptime cmd: Command.Tag) !void {
return loadConfig_(allocator, user_config_path_, ctx, cmd, false);
}

pub fn loadConfig_(
allocator: std.mem.Allocator,
user_config_path_: ?string,
ctx: Command.Context,
comptime cmd: Command.Tag,
override_absolute_working_dir: ?bool,
) !void {
var config_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
if (comptime cmd.readGlobalConfig()) {
if (!ctx.has_loaded_global_config) {
Expand All @@ -310,7 +323,6 @@ pub const Arguments = struct {
}

var config_path_: []const u8 = user_config_path_ orelse "";

var auto_loaded: bool = false;
if (config_path_.len == 0 and (user_config_path_ != null or
Command.Tag.always_loads_config.get(cmd) or
Expand All @@ -334,7 +346,7 @@ pub const Arguments = struct {
config_buf[config_path_.len] = 0;
config_path = config_buf[0..config_path_.len :0];
} else {
if (ctx.args.absolute_working_dir == null) {
if ((override_absolute_working_dir orelse false) or ctx.args.absolute_working_dir == null) {
var secondbuf: [bun.MAX_PATH_BYTES]u8 = undefined;
const cwd = bun.getcwd(&secondbuf) catch return;

Expand All @@ -348,6 +360,7 @@ pub const Arguments = struct {
&parts,
.auto,
);

config_buf[config_path_.len] = 0;
config_path = config_buf[0..config_path_.len :0];
}
Expand Down Expand Up @@ -1447,6 +1460,7 @@ pub const Command = struct {
},
.InstallCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .InstallCommand) unreachable;

const ctx = try Command.init(allocator, log, .InstallCommand);

try InstallCommand.exec(ctx);
Expand Down Expand Up @@ -2224,7 +2238,7 @@ pub const Command = struct {
Output.flush();
},
Command.Tag.HelpCommand => {
HelpCommand.printWithReason(.explicit);
HelpCommand.printWithReason(.explicit, show_all_flags);
},
Command.Tag.UpgradeCommand => {
const intro_text =
Expand Down Expand Up @@ -2283,7 +2297,7 @@ pub const Command = struct {
Output.flush();
},
else => {
HelpCommand.printWithReason(.explicit);
HelpCommand.printWithReason(.explicit, show_all_flags);
},
}
}
Expand Down Expand Up @@ -2329,14 +2343,14 @@ pub const Command = struct {

pub const uses_global_options: std.EnumArray(Tag, bool) = std.EnumArray(Tag, bool).initDefault(true, .{
.CreateCommand = false,
.BunxCommand = false,
.InstallCommand = false,
.AddCommand = false,
.RemoveCommand = false,
.UpdateCommand = false,
.PackageManagerCommand = false,
.LinkCommand = false,
.UnlinkCommand = false,
.BunxCommand = false,
});
};
};
Expand Down
40 changes: 36 additions & 4 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6978,6 +6978,7 @@ pub const PackageManager = struct {

pub fn init(ctx: Command.Context, comptime subcommand: Subcommand) !*PackageManager {
const cli = try CommandLineArguments.parse(ctx.allocator, subcommand);

return initWithCLI(ctx, cli, subcommand);
}

Expand Down Expand Up @@ -7010,6 +7011,8 @@ pub const PackageManager = struct {

var workspace_names = Package.WorkspaceMap.init(ctx.allocator);

const before_top_level_dir = try ctx.allocator.dupe(u8, fs.top_level_dir);

// Step 1. Find the nearest package.json directory
//
// We will walk up from the cwd, trying to find the nearest package.json file.
Expand Down Expand Up @@ -7082,9 +7085,24 @@ pub const PackageManager = struct {
};

const child_cwd = this_cwd;

// Reading a first time the config to know if we need to ignore the workspace or not
try BunArguments.loadConfig(ctx.allocator, cli.config, ctx, .InstallCommand);

var ignore_workspace = false;

if (cli.ignore_workspace) {
ignore_workspace = true;
}

if (ctx.install) |install_| {
ignore_workspace = install_.ignore_workspace orelse false;
}

// Check if this is a workspace; if so, use root package
var found = false;
if (comptime subcommand != .link) {

if ((comptime subcommand != .link) and !ignore_workspace) {
if (!created_package_json) {
while (std.fs.path.dirname(this_cwd)) |parent| : (this_cwd = parent) {
const parent_without_trailing_slash = strings.withoutTrailingSlash(parent);
Expand Down Expand Up @@ -7152,8 +7170,15 @@ pub const PackageManager = struct {
break :brk child_json;
};

try bun.sys.chdir(fs.top_level_dir).unwrap();
try BunArguments.loadConfig(ctx.allocator, cli.config, ctx, .InstallCommand);
// If the top level dir is different than before, then we don't ignore the workspace and we found one,
// so we need to read again the config and override the working dir
if (!strings.eql(before_top_level_dir, fs.top_level_dir)) {
try bun.sys.chdir(fs.top_level_dir).unwrap();
ctx.args.absolute_working_dir = try ctx.allocator.dupe(u8, fs.top_level_dir);

try BunArguments.loadConfig_(ctx.allocator, cli.config, ctx, .InstallCommand, true);
}

bun.copy(u8, &cwd_buf, fs.top_level_dir);
cwd_buf[fs.top_level_dir.len] = '/';
cwd_buf[fs.top_level_dir.len + 1] = 0;
Expand Down Expand Up @@ -7278,6 +7303,7 @@ pub const PackageManager = struct {

break :brk @as(u32, @truncate(@as(u64, @intCast(@max(std.time.timestamp(), 0)))));
};

return manager;
}

Expand Down Expand Up @@ -7773,6 +7799,7 @@ pub const PackageManager = struct {
clap.parseParam("--no-progress Disable the progress bar") catch unreachable,
clap.parseParam("--no-summary Don't print a summary") catch unreachable,
clap.parseParam("--no-verify Skip verifying integrity of newly downloaded packages") catch unreachable,
clap.parseParam("--ignore-workspace Skip verifying if the current package is in a workspace or not") catch unreachable,
clap.parseParam("--ignore-scripts Skip lifecycle scripts in the project's package.json (dependency scripts are never run)") catch unreachable,
clap.parseParam("--trust Add to trustedDependencies in the project's package.json and install the package(s)") catch unreachable,
clap.parseParam("-g, --global Install globally") catch unreachable,
Expand Down Expand Up @@ -7849,6 +7876,7 @@ pub const PackageManager = struct {
ignore_scripts: bool = false,
trusted: bool = false,
no_summary: bool = false,
ignore_workspace: bool = false,

link_native_bins: []const string = &[_]string{},

Expand Down Expand Up @@ -8070,6 +8098,7 @@ pub const PackageManager = struct {
cli.ignore_scripts = args.flag("--ignore-scripts");
cli.trusted = args.flag("--trust");
cli.no_summary = args.flag("--no-summary");
cli.ignore_workspace = args.flag("--ignore-workspace");

// link and unlink default to not saving, all others default to
// saving.
Expand Down Expand Up @@ -8124,6 +8153,7 @@ pub const PackageManager = struct {
buf[cwd_.len] = 0;
final_path = buf[0..cwd_.len :0];
}

try bun.sys.chdir(final_path).unwrap();
}

Expand Down Expand Up @@ -8988,7 +9018,7 @@ pub const PackageManager = struct {

// pub fn printTreeDeps(this: *PackageInstaller) void {
// for (this.tree_ids_to_trees_the_id_depends_on, 0..) |deps, j| {
// std.debug.print("tree #{d:3}: ", .{j});
// std.debug.print("tree #{d:3}: ", .{j});
// for (0..this.lockfile.buffers.trees.items.len) |tree_id| {
// std.debug.print("{d} ", .{@intFromBool(deps.isSet(tree_id))});
// }
Expand Down Expand Up @@ -10674,9 +10704,11 @@ pub const PackageManager = struct {
_ = manager.getCacheDirectory();
_ = manager.getTemporaryDirectory();
}

manager.enqueueDependencyList(root.dependencies);
} else {
// Anything that needs to be downloaded from an update needs to be scheduled here

manager.drainDependencyList();
}

Expand Down