1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

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> { pub fn discover(flags: &crate::Flags) -> Result<Option<ConfigFile>, AnyError> {
if let Some(config_path) = flags.config_path.as_ref() { if let Some(config_path) = flags.config_path.as_ref() {
Ok(Some(ConfigFile::read(config_path)?)) Ok(Some(ConfigFile::read(config_path)?))
} else { } else if let Some(config_path_args) = flags.config_path_args() {
let mut checked = HashSet::new(); 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)? { if let Some(cf) = discover_from(&f, &mut checked)? {
return Ok(Some(cf)); return Ok(Some(cf));
} }
} }
// From CWD walk up to root looking for deno.json or deno.jsonc // From CWD walk up to root looking for deno.json or deno.jsonc
let cwd = std::env::current_dir()?; let cwd = std::env::current_dir()?;
discover_from(&cwd, &mut checked) discover_from(&cwd, &mut checked)
} else {
Ok(None)
} }
} }

View file

@ -373,24 +373,33 @@ impl Flags {
} }
/// Extract path arguments for config search paths. /// 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::*; use DenoSubcommand::*;
if let Fmt(FmtFlags { files, .. }) = &self.subcommand { if let Fmt(FmtFlags { files, .. }) = &self.subcommand {
files.clone() Some(files.clone())
} else if let Lint(LintFlags { files, .. }) = &self.subcommand { } else if let Lint(LintFlags { files, .. }) = &self.subcommand {
files.clone() Some(files.clone())
} else if let Run(RunFlags { script }) = &self.subcommand { } else if let Run(RunFlags { script }) = &self.subcommand {
if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) { if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) {
if module_specifier.scheme() == "file" {
if let Ok(p) = module_specifier.to_file_path() { if let Ok(p) = module_specifier.to_file_path() {
vec![p] Some(vec![p])
} else { } else {
vec![] Some(vec![])
} }
} else { } 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 { } else {
vec![] Some(vec![])
}
} else {
Some(vec![])
} }
} }
@ -4942,24 +4951,29 @@ mod tests {
let flags = flags_from_vec(svec!["deno", "run", "foo.js"]).unwrap(); let flags = flags_from_vec(svec!["deno", "run", "foo.js"]).unwrap();
assert_eq!( assert_eq!(
flags.config_path_args(), 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 = let flags =
flags_from_vec(svec!["deno", "lint", "dir/a.js", "dir/b.js"]).unwrap(); flags_from_vec(svec!["deno", "lint", "dir/a.js", "dir/b.js"]).unwrap();
assert_eq!( assert_eq!(
flags.config_path_args(), 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(); 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 = let flags =
flags_from_vec(svec!["deno", "fmt", "dir/a.js", "dir/b.js"]).unwrap(); flags_from_vec(svec!["deno", "fmt", "dir/a.js", "dir/b.js"]).unwrap();
assert_eq!( assert_eq!(
flags.config_path_args(), 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", args: "run colors_without_globalThis.js",
output_str: Some("true\n"), 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");