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

refactor: move FileCollector to deno_config #24433

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move vendor folder resolution into deno_config
  • Loading branch information
dsherret committed Jul 4, 2024
commit 6c8768947707d41a127831b63e3714d315a8dff8
60 changes: 15 additions & 45 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,7 @@ use deno_config::workspace::PackageJsonDepResolution;
use deno_config::workspace::Workspace;
use deno_config::workspace::WorkspaceDiscoverOptions;
use deno_config::workspace::WorkspaceDiscoverStart;
use deno_config::workspace::WorkspaceEmptyOptions;
use deno_config::workspace::WorkspaceMemberContext;
use deno_config::workspace::WorkspaceResolver;
use deno_config::WorkspaceLintConfig;
Expand Down Expand Up @@ -778,7 779,6 @@ pub struct CliOptions {
flags: Flags,
initial_cwd: PathBuf,
maybe_node_modules_folder: Option<PathBuf>,
maybe_vendor_folder: Option<PathBuf>,
npmrc: Arc<ResolvedNpmRc>,
maybe_lockfile: Option<Arc<CliLockfile>>,
overrides: CliOptionOverrides,
Expand Down Expand Up @@ -822,15 822,6 @@ impl CliOptions {
root_folder.pkg_json.as_deref(),
)
.with_context(|| "Resolving node_modules folder.")?;
let maybe_vendor_folder = if force_global_cache {
None
} else {
resolve_vendor_folder(
&initial_cwd,
&flags,
root_folder.deno_json.as_deref(),
)
};

if let Some(env_file_name) = &flags.env_file {
match from_filename(env_file_name) {
Expand Down Expand Up @@ -859,7 850,6 @@ impl CliOptions {
maybe_lockfile,
npmrc,
maybe_node_modules_folder,
maybe_vendor_folder,
overrides: Default::default(),
workspace,
disable_deprecated_api_warning,
Expand All @@ -871,6 861,7 @@ impl CliOptions {
let initial_cwd =
std::env::current_dir().with_context(|| "Failed getting cwd.")?;
let config_fs_adapter = DenoConfigFsAdapter::new(&RealFs);
let vendor_flag = flags.vendor;
let resolve_workspace_discover_options = || {
let additional_config_file_names: &'static [&'static str] =
if matches!(flags.subcommand, DenoSubcommand::Publish(..)) {
Expand Down Expand Up @@ -899,8 890,15 @@ impl CliOptions {
config_parse_options,
additional_config_file_names,
discover_pkg_json,
use_vendor_folder_override: vendor_flag,
}
};
let resolve_empty_options = || WorkspaceEmptyOptions {
root_dir: Arc::new(
ModuleSpecifier::from_directory_path(&initial_cwd).unwrap(),
),
use_vendor_dir: vendor_flag.unwrap_or(false),
};

let workspace = match &flags.config_flag {
deno_config::ConfigFlag::Discover => {
Expand All @@ -910,9 908,7 @@ impl CliOptions {
&resolve_workspace_discover_options(),
)?
} else {
Workspace::empty(Arc::new(
ModuleSpecifier::from_directory_path(&initial_cwd).unwrap(),
))
Workspace::empty(resolve_empty_options())
}
}
deno_config::ConfigFlag::Path(path) => {
Expand All @@ -922,9 918,9 @@ impl CliOptions {
&resolve_workspace_discover_options(),
)?
}
deno_config::ConfigFlag::Disabled => Workspace::empty(Arc::new(
ModuleSpecifier::from_directory_path(&initial_cwd).unwrap(),
)),
deno_config::ConfigFlag::Disabled => {
Workspace::empty(resolve_empty_options())
}
};

for diagnostic in workspace.diagnostics() {
Expand Down Expand Up @@ -1262,7 1258,6 @@ impl CliOptions {
flags: self.flags.clone(),
initial_cwd: self.initial_cwd.clone(),
maybe_node_modules_folder: Some(path),
maybe_vendor_folder: self.maybe_vendor_folder.clone(),
npmrc: self.npmrc.clone(),
maybe_lockfile: self.maybe_lockfile.clone(),
workspace: self.workspace.clone(),
Expand All @@ -1279,8 1274,8 @@ impl CliOptions {
.or_else(|| self.workspace.node_modules_dir())
}

pub fn vendor_dir_path(&self) -> Option<&PathBuf> {
self.maybe_vendor_folder.as_ref()
pub fn vendor_dir_path(&self) -> Option<PathBuf> {
self.workspace.vendor_dir_path()
}

pub fn resolve_root_cert_store_provider(
Expand Down Expand Up @@ -1805,31 1800,6 @@ fn resolve_node_modules_folder(
Ok(Some(canonicalize_path_maybe_not_exists(&path)?))
}

fn resolve_vendor_folder(
cwd: &Path,
flags: &Flags,
maybe_config_file: Option<&ConfigFile>,
) -> Option<PathBuf> {
let use_vendor_dir = flags
.vendor
.or_else(|| maybe_config_file.and_then(|c| c.json.vendor))
.unwrap_or(false);
// Unlike the node_modules directory, there is no need to canonicalize
// this directory because it's just used as a cache and the resolved
// specifier is not based on the canonicalized path (unlike the modules
// in the node_modules folder).
if !use_vendor_dir {
None
} else if let Some(config_path) = maybe_config_file
.as_ref()
.and_then(|c| c.specifier.to_file_path().ok())
{
Some(config_path.parent().unwrap().join("vendor"))
} else {
Some(cwd.join("vendor"))
}
}

fn resolve_import_map_specifier(
maybe_import_map_path: Option<&str>,
maybe_config_file: Option<&ConfigFile>,
Expand Down
2 changes: 1 addition & 1 deletion cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 502,7 @@ impl CliFactory {
.options
.workspace
.to_maybe_jsx_import_source_config()?,
maybe_vendor_dir: self.options.vendor_dir_path(),
maybe_vendor_dir: self.options.vendor_dir_path().as_ref(),
})))
}
.boxed_local(),
Expand Down
8 changes: 7 additions & 1 deletion cli/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 1299,13 @@ impl ConfigData {
}
};

let vendor_dir = config_file.as_ref().and_then(|c| c.vendor_dir_path());
let vendor_dir = config_file.as_ref().and_then(|c| {
if c.vendor() == Some(true) {
Some(c.specifier.to_file_path().ok()?.parent()?.join("vendor"))
} else {
None
}
});

// Load lockfile
let lockfile = config_file.as_ref().and_then(resolve_lockfile_from_config);
Expand Down
5 changes: 5 additions & 0 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3568,6 3568,11 @@ impl Inner {
},
additional_config_file_names: &[],
discover_pkg_json: true,
use_vendor_folder_override: if force_global_cache {
Some(false)
} else {
None
},
},
)?);
let cli_options = CliOptions::new(
Expand Down
4 changes: 2 additions & 2 deletions cli/tools/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 424,7 @@ pub async fn run_benchmarks(
.map(|(_, bench_options)| {
collect_specifiers(
bench_options.files.clone(),
cli_options.vendor_dir_path().map(ToOwned::to_owned),
cli_options.vendor_dir_path(),
is_supported_bench_path,
)
})
Expand Down Expand Up @@ -509,7 509,7 @@ pub async fn run_benchmarks_with_watch(
.map(|(_, bench_options)| {
collect_specifiers(
bench_options.files.clone(),
cli_options.vendor_dir_path().map(ToOwned::to_owned),
cli_options.vendor_dir_path(),
is_supported_bench_path,
)
})
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 407,7 @@ fn collect_coverages(
})
.ignore_git_folder()
.ignore_node_modules()
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
.set_vendor_folder(cli_options.vendor_dir_path())
.collect_file_patterns(&deno_config::fs::RealDenoConfigFs, file_patterns)?;

let coverage_patterns = FilePatterns {
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 115,7 @@ pub async fn doc(flags: Flags, doc_flags: DocFlags) -> Result<(), AnyError> {
),
exclude: Default::default(),
},
cli_options.vendor_dir_path().map(ToOwned::to_owned),
cli_options.vendor_dir_path(),
|_| true,
)?;
let graph = module_graph_creator
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 199,7 @@ fn collect_fmt_files(
FileCollector::new(|e| is_supported_ext_fmt(e.path))
.ignore_git_folder()
.ignore_node_modules()
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
.set_vendor_folder(cli_options.vendor_dir_path())
.collect_file_patterns(&deno_config::fs::RealDenoConfigFs, files)
}

Expand Down
2 changes: 1 addition & 1 deletion cli/tools/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 400,7 @@ fn collect_lint_files(
FileCollector::new(|e| is_script_ext(e.path))
.ignore_git_folder()
.ignore_node_modules()
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
.set_vendor_folder(cli_options.vendor_dir_path())
.collect_file_patterns(&deno_config::fs::RealDenoConfigFs, files)
}

Expand Down
2 changes: 1 addition & 1 deletion cli/tools/registry/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 339,7 @@ fn collect_paths(
})
.ignore_git_folder()
.ignore_node_modules()
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
.set_vendor_folder(cli_options.vendor_dir_path())
.use_gitignore()
.collect_file_patterns(&deno_config::fs::RealDenoConfigFs, file_patterns)
}
12 changes: 5 additions & 7 deletions cli/tools/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,16 1660,14 @@ fn collect_specifiers_with_test_mode(
let vendor_folder = cli_options.vendor_dir_path();
let module_specifiers = collect_specifiers(
files.clone(),
vendor_folder.map(ToOwned::to_owned),
vendor_folder.clone(),
is_supported_test_path_predicate,
)?;

if *include_inline {
return collect_specifiers(
files,
vendor_folder.map(ToOwned::to_owned),
|e| is_supported_test_ext(e.path),
)
return collect_specifiers(files, vendor_folder.clone(), |e| {
is_supported_test_ext(e.path)
})
.map(|specifiers| {
specifiers
.into_iter()
Expand Down Expand Up @@ -1875,7 1873,7 @@ pub async fn run_tests_with_watch(
.map(|(_, test_options)| {
collect_specifiers(
test_options.files.clone(),
cli_options.vendor_dir_path().map(ToOwned::to_owned),
cli_options.vendor_dir_path(),
if workspace_test_options.doc {
Box::new(|e: WalkEntry| is_supported_test_ext(e.path))
as Box<dyn Fn(WalkEntry) -> bool>
Expand Down