summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2022-02-25 14:39:18 +0900
committerGitHub <noreply@github.com>2022-02-25 14:39:18 +0900
commit111c343281b559ea51fd66c2ddc260549406a822 (patch)
tree28cb914bc72b6c3f390ef12250c02c9a6177bcdd
parent3b12afd0723b288feb7c8c53ac3938a17fd0e57c (diff)
fix(cli): disable config discovery for remote script (#13745)
-rw-r--r--cli/config_file.rs7
-rw-r--r--cli/flags.rs38
-rw-r--r--cli/tests/integration/run_tests.rs11
-rw-r--r--cli/tests/testdata/run/with_config/deno.jsonc6
-rw-r--r--cli/tests/testdata/run/with_config/frontend_work.ts4
-rw-r--r--cli/tests/testdata/run/with_config/server_side_work.ts2
6 files changed, 53 insertions, 15 deletions
diff --git a/cli/config_file.rs b/cli/config_file.rs
index 689a08ab3..9d5ae84a0 100644
--- a/cli/config_file.rs
+++ b/cli/config_file.rs
@@ -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)
}
}
diff --git a/cli/flags.rs b/cli/flags.rs
index 316425247..6c3186c55 100644
--- a/cli/flags.rs
+++ b/cli/flags.rs
@@ -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")])
);
}
diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs
index e8ee10065..beb5e8cae 100644
--- a/cli/tests/integration/run_tests.rs
+++ b/cli/tests/integration/run_tests.rs
@@ -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,
+});
diff --git a/cli/tests/testdata/run/with_config/deno.jsonc b/cli/tests/testdata/run/with_config/deno.jsonc
new file mode 100644
index 000000000..9017fac30
--- /dev/null
+++ b/cli/tests/testdata/run/with_config/deno.jsonc
@@ -0,0 +1,6 @@
+{
+ // type settings for frontend dev
+ "compilerOptions": {
+ "lib": ["esnext", "dom"]
+ }
+}
diff --git a/cli/tests/testdata/run/with_config/frontend_work.ts b/cli/tests/testdata/run/with_config/frontend_work.ts
new file mode 100644
index 000000000..783af44e4
--- /dev/null
+++ b/cli/tests/testdata/run/with_config/frontend_work.ts
@@ -0,0 +1,4 @@
+function _main() {
+ console.log(document);
+}
+console.log("ok");
diff --git a/cli/tests/testdata/run/with_config/server_side_work.ts b/cli/tests/testdata/run/with_config/server_side_work.ts
new file mode 100644
index 000000000..12db2ab05
--- /dev/null
+++ b/cli/tests/testdata/run/with_config/server_side_work.ts
@@ -0,0 +1,2 @@
+const _ = Deno.build.os;
+console.log("ok");