Skip to content

Commit

Permalink
core: clearly define when module lookup is path-based vs URL-based
Browse files Browse the repository at this point in the history
The rules are now as follows:

* In `import` statements, as mandated by the WHATWG specification,
  the import specifier is always treated as a URL.
  If it is a relative URL, it must start with either / or ./ or ../

* A script name passed to deno as a command line argument may be either
  an absolute URL or a local path.
  - If the name starts with a valid URI scheme followed by a colon, e.g.
    'http:', 'https:', 'file:', 'foo+bar:', it always interpreted as a
    URL (even if Deno doesn't support the indicated protocol).
  - Otherwise, the script name is interpreted as a local path. The local
    path may be relative, and operating system semantics determine how
    it is resolved. Prefixing a relative path with ./ is not required.
  • Loading branch information
piscisaureus committed Jul 8, 2019
1 parent 92ac616 commit fb9bb51
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cli/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
tokio_util::init(|| {
let specifier = "./tests/002_hello.ts";
use deno::ModuleSpecifier;
let module_name = ModuleSpecifier::resolve_root(specifier)
let module_name = ModuleSpecifier::resolve_url_or_path(specifier)
.unwrap()
.to_string();

Expand Down Expand Up @@ -296,7 +296,7 @@ mod tests {
fn test_bundle_async() {
let specifier = "./tests/002_hello.ts";
use deno::ModuleSpecifier;
let module_name = ModuleSpecifier::resolve_root(specifier)
let module_name = ModuleSpecifier::resolve_url_or_path(specifier)
.unwrap()
.to_string();

Expand Down
3 changes: 2 additions & 1 deletion cli/deno_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ impl DenoError {
use ModuleResolutionError::*;
match err {
InvalidUrl(err) | InvalidBaseUrl(err) => Self::url_error_kind(err),
ImportPathPrefixMissing => ErrorKind::ImportPathPrefixMissing,
InvalidPath => ErrorKind::InvalidPath,
ImportPrefixMissing => ErrorKind::ImportPrefixMissing,
}
}
Repr::Diagnostic(ref _err) => ErrorKind::Diagnostic,
Expand Down
4 changes: 3 additions & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,9 @@ pub enum DenoSubcommand {

fn get_default_bundle_filename(source_file: &str) -> String {
use deno::ModuleSpecifier;
let url = ModuleSpecifier::resolve_root(source_file).unwrap().to_url();
let url = ModuleSpecifier::resolve_url_or_path(source_file)
.unwrap()
.to_url();
let path_segments = url.path_segments().unwrap();
let last = path_segments.last().unwrap();
String::from(last.trim_end_matches(".ts").trim_end_matches(".js"))
Expand Down
4 changes: 1 addition & 3 deletions cli/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ impl ImportMap {
continue;
}

let normalized_address = ModuleSpecifier::resolve(&url_string, ".")
.expect("Address should be valid module specifier");
normalized_addresses.push(normalized_address);
normalized_addresses.push(url.into());
}

normalized_addresses
Expand Down
3 changes: 2 additions & 1 deletion cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ enum ErrorKind: byte {
WouldBlock,
InvalidInput,
InvalidData,
InvalidPath,
TimedOut,
Interrupted,
WriteZero,
Expand Down Expand Up @@ -141,7 +142,7 @@ enum ErrorKind: byte {
NoAsyncSupport,
NoSyncSupport,
ImportMapError,
ImportPathPrefixMissing,
ImportPrefixMissing,

// other kinds
Diagnostic,
Expand Down
2 changes: 1 addition & 1 deletion cli/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ fn op_create_worker(
err_check(worker.execute("denoMain()"));
err_check(worker.execute("workerMain()"));

let module_specifier = ModuleSpecifier::resolve_root(specifier)?;
let module_specifier = ModuleSpecifier::resolve_url_or_path(specifier)?;

let op =
worker
Expand Down
5 changes: 3 additions & 2 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ impl Loader for ThreadSafeState {
}
}

ModuleSpecifier::resolve(specifier, referrer).map_err(DenoError::from)
ModuleSpecifier::resolve_import(specifier, referrer)
.map_err(DenoError::from)
}

/// Given an absolute url, load its source code.
Expand Down Expand Up @@ -252,7 +253,7 @@ impl ThreadSafeState {
None
} else {
let root_specifier = argv_rest[1].clone();
match ModuleSpecifier::resolve_root(&root_specifier) {
match ModuleSpecifier::resolve_url_or_path(&root_specifier) {
Ok(specifier) => Some(specifier),
Err(e) => {
// TODO: handle unresolvable specifier
Expand Down
10 changes: 5 additions & 5 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
#[test]
fn execute_mod_esm_imports_a() {
let module_specifier =
ModuleSpecifier::resolve_root("tests/esm_imports_a.js").unwrap();
ModuleSpecifier::resolve_url_or_path("tests/esm_imports_a.js").unwrap();
let argv = vec![String::from("./deno"), module_specifier.to_string()];
let state = ThreadSafeState::new(
flags::DenoFlags::default(),
Expand Down Expand Up @@ -169,7 +169,7 @@ mod tests {
#[test]
fn execute_mod_circular() {
let module_specifier =
ModuleSpecifier::resolve_root("tests/circular1.js").unwrap();
ModuleSpecifier::resolve_url_or_path("tests/circular1.js").unwrap();
let argv = vec![String::from("./deno"), module_specifier.to_string()];
let state = ThreadSafeState::new(
flags::DenoFlags::default(),
Expand Down Expand Up @@ -197,7 +197,7 @@ mod tests {
#[test]
fn execute_006_url_imports() {
let module_specifier =
ModuleSpecifier::resolve_root("tests/006_url_imports.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("tests/006_url_imports.ts").unwrap();
let argv = vec![String::from("deno"), module_specifier.to_string()];
let mut flags = flags::DenoFlags::default();
flags.reload = true;
Expand Down Expand Up @@ -327,7 +327,7 @@ mod tests {
// "foo" is not a valid module specifier so this should return an error.
let mut worker = create_test_worker();
let module_specifier =
ModuleSpecifier::resolve_root("does-not-exist").unwrap();
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
let result = worker.execute_mod_async(&module_specifier, false).wait();
assert!(result.is_err());
})
Expand All @@ -340,7 +340,7 @@ mod tests {
// tests).
let mut worker = create_test_worker();
let module_specifier =
ModuleSpecifier::resolve_root("./tests/002_hello.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("./tests/002_hello.ts").unwrap();
let result = worker.execute_mod_async(&module_specifier, false).wait();
assert!(result.is_ok());
})
Expand Down
Loading

0 comments on commit fb9bb51

Please sign in to comment.