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

feat: add suggestions to module not found error messages for file urls #21498

Merged

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Dec 7, 2023

For example:

> cat example.ts
import { Example } from "./routes";
console.log(Example);
> ../deno/target/debug/deno run example.ts                          
error: Module not found "file:https:///V:/scratch/routes". Maybe add a '.ts' extension or run with --unstable-sloppy-imports
    at file:https:///V:/scratch/example.ts:1:25
> ../deno/target/debug/deno run --unstable-sloppy-imports example.ts
Warning Sloppy import resolution (hint: add .ts extension)
    at file:https:///V:/scratch/example.ts:1:25
[class Example]

@dsherret dsherret changed the title feat: add more suggestions to module not found error messages for file urls feat: add suggestions to module not found error messages for file urls Dec 7, 2023
@ry
Copy link
Member

ry commented Dec 7, 2023

are there any performance impacts? startup time?

@dsherret
Copy link
Member Author

dsherret commented Dec 7, 2023

This code is only executed on error.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I was wondering if we should print out a warning when --unstable-sllopy-imports is passed to inform users that it will be slower without it and they should consider removing this flag if no other warning is printed. But we can certainly do that in another PR.

Comment on lines +977 to +1016
fn test_sloppy_import_resolution_to_message() {
// none
let url = ModuleSpecifier::parse("file:https:///dir/index.js").unwrap();
assert_eq!(
sloppy_import_resolution_to_suggestion_message(
&SloppyImportsResolution::None(&url)
),
None,
);
// directory
assert_eq!(
sloppy_import_resolution_to_suggestion_message(
&SloppyImportsResolution::Directory(
ModuleSpecifier::parse("file:https:///dir/index.js").unwrap()
)
)
.unwrap(),
"Maybe specify path to 'index.js' file in directory instead"
);
// no ext
assert_eq!(
sloppy_import_resolution_to_suggestion_message(
&SloppyImportsResolution::NoExtension(
ModuleSpecifier::parse("file:https:///dir/index.mjs").unwrap()
)
)
.unwrap(),
"Maybe add a '.mjs' extension"
);
// js to ts
assert_eq!(
sloppy_import_resolution_to_suggestion_message(
&SloppyImportsResolution::JsToTs(
ModuleSpecifier::parse("file:https:///dir/index.mts").unwrap()
)
)
.unwrap(),
"Maybe change the extension to '.mts'"
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍

Comment on lines +10710 to +10716
code: Some(lsp::NumberOrString::String("no-local".to_string())),
source: Some("deno".to_string()),
message: format!(
"Unable to load a local module: {}\nMaybe add a '.ts' extension.",
temp_dir.join("a").uri_file(),
),
..Default::default()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is super nice 👍 will it still provide a quick-fix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at the moment. I will do that in a follow-up sometime.

@dsherret
Copy link
Member Author

dsherret commented Dec 7, 2023

I was wondering if we should print out a warning when --unstable-sllopy-imports is passed to inform users that it will be slower without it and they should consider removing this flag if no other warning is printed. But we can certainly do that in another PR.

I was thinking about that, but it already prints a warning for every import.

@bartlomieju
Copy link
Member

Right, but what if you fix all these errors and still run with the flag. Won't that cause unnecessary computation?

@dsherret
Copy link
Member Author

dsherret commented Dec 7, 2023

Right, but what if you fix all these errors and still run with the flag. Won't that cause unnecessary computation?

Yeah, good point. I'll do another PR that adds this soon.

@dsherret dsherret merged commit 7856675 into denoland:main Dec 7, 2023
14 checks passed
@dsherret dsherret deleted the feat_suggestions_for_sloppy_imports branch December 7, 2023 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants