fix(cli): disable config discovery for remote script (#13745)

This commit is contained in:
Yoshiya Hinosawa 2022-02-25 14:39:18 +09:00 committed by GitHub
parent 3b12afd072
commit 111c343281
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 15 deletions

View File

@ -164,17 +164,18 @@ const CONFIG_FILE_NAMES: [&str; 2] = ["deno.json", "deno.jsonc"];
pub fn discover(flags: &crate::Flags) -> Result<Option<ConfigFile>, AnyError> {
if let Some(config_path) = flags.config_path.as_ref() {
Ok(Some(ConfigFile::read(config_path)?))
} else {
} else if let Some(config_path_args) = flags.config_path_args() {
let mut checked = HashSet::new();
for f in flags.config_path_args() {
for f in config_path_args {
if let Some(cf) = discover_from(&f, &mut checked)? {
return Ok(Some(cf));
}
}
// From CWD walk up to root looking for deno.json or deno.jsonc
let cwd = std::env::current_dir()?;
discover_from(&cwd, &mut checked)
} else {
Ok(None)
}
}

View File

@ -373,24 +373,33 @@ impl Flags {
}
/// Extract path arguments for config search paths.
pub fn config_path_args(&self) -> Vec<PathBuf> {
/// If it returns Some(vec), the config should be discovered
/// from the current dir after trying to discover from each entry in vec.
/// If it returns None, the config file shouldn't be discovered at all.
pub fn config_path_args(&self) -> Option<Vec<PathBuf>> {
use DenoSubcommand::*;
if let Fmt(FmtFlags { files, .. }) = &self.subcommand {
files.clone()
Some(files.clone())
} else if let Lint(LintFlags { files, .. }) = &self.subcommand {
files.clone()
Some(files.clone())
} else if let Run(RunFlags { script }) = &self.subcommand {
if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) {
if let Ok(p) = module_specifier.to_file_path() {
vec![p]
if module_specifier.scheme() == "file" {
if let Ok(p) = module_specifier.to_file_path() {
Some(vec![p])
} else {
Some(vec![])
}
} else {
vec![]
// When the entrypoint doesn't have file: scheme (it's the remote
// script), then we don't auto discover config file.
None
}
} else {
vec![]
Some(vec![])
}
} else {
vec![]
Some(vec![])
}
}
@ -4942,24 +4951,29 @@ mod tests {
let flags = flags_from_vec(svec!["deno", "run", "foo.js"]).unwrap();
assert_eq!(
flags.config_path_args(),
vec![std::env::current_dir().unwrap().join("foo.js")]
Some(vec![std::env::current_dir().unwrap().join("foo.js")])
);
let flags =
flags_from_vec(svec!["deno", "run", "https://example.com/foo.js"])
.unwrap();
assert_eq!(flags.config_path_args(), None);
let flags =
flags_from_vec(svec!["deno", "lint", "dir/a.js", "dir/b.js"]).unwrap();
assert_eq!(
flags.config_path_args(),
vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]
Some(vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")])
);
let flags = flags_from_vec(svec!["deno", "lint"]).unwrap();
assert!(flags.config_path_args().is_empty());
assert!(flags.config_path_args().unwrap().is_empty());
let flags =
flags_from_vec(svec!["deno", "fmt", "dir/a.js", "dir/b.js"]).unwrap();
assert_eq!(
flags.config_path_args(),
vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]
Some(vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")])
);
}

View File

@ -2500,3 +2500,14 @@ itest!(colors_without_global_this {
args: "run colors_without_globalThis.js",
output_str: Some("true\n"),
});
itest!(config_auto_discovered_for_local_script {
args: "run --quiet run/with_config/frontend_work.ts",
output_str: Some("ok\n"),
});
itest!(config_not_auto_discovered_for_remote_script {
args: "run --quiet http://127.0.0.1:4545/run/with_config/server_side_work.ts",
output_str: Some("ok\n"),
http_server: true,
});

View File

@ -0,0 +1,6 @@
{
// type settings for frontend dev
"compilerOptions": {
"lib": ["esnext", "dom"]
}
}

View File

@ -0,0 +1,4 @@
function _main() {
console.log(document);
}
console.log("ok");

View File

@ -0,0 +1,2 @@
const _ = Deno.build.os;
console.log("ok");