-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(resolver): support baseUrl in tsconfig.json
#7263
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
Changes from 1 commit
ffcbe88
e57e171
b6f4560
afba8bc
bcace8e
b134da1
b99716b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| "@biomejs/biome": minor | ||
| --- | ||
|
|
||
| Biome's resolver now supports `baseUrl` if specified in `tsconfig.json`. This | ||
| means that the following now resolves: | ||
|
|
||
| **`tsconfig.json`** | ||
| ```json | ||
| { | ||
| "compilerOptions": { | ||
| "baseUrl": "./src", | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **`index.ts`** | ||
| ```ts | ||
| import { foo } from "foo"; // This will now work. | ||
| ``` | ||
|
|
||
| **`src/foo.ts`** | ||
| ```ts | ||
| export function foo() {} | ||
| ``` | ||
|
|
||
| Fixes [#6432](https://github.com/biomejs/biome/issues/6432). | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,14 +86,19 @@ impl TsConfigJson { | |||||||||||||||||||
|
|
||||||||||||||||||||
| #[derive(Clone, Debug, Default, Deserializable)] | ||||||||||||||||||||
| pub struct CompilerOptions { | ||||||||||||||||||||
| /// https://www.typescriptlang.org/tsconfig/#baseUrl | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// The base URL is normalised to an absolute path after parsing. | ||||||||||||||||||||
| pub base_url: Option<Utf8PathBuf>, | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+111
to
115
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainEnsure JSON key maps to base_url (baseUrl) during deserialisation Unless the derive macro auto-maps camelCase to snake_case, Apply this diff: /// https://www.typescriptlang.org/tsconfig/#baseUrl
///
/// The base URL is normalised to an absolute path after parsing.
- pub base_url: Option<Utf8PathBuf>,
+ #[deserializable(rename = "baseUrl")]
+ pub base_url: Option<Utf8PathBuf>,To confirm whether auto-mapping is already handled, run: 🏁 Script executed: #!/bin/bash
# Inspect how camelCase keys are handled elsewhere and if baseUrl mapping exists
rg -n -C2 --type=rust 'Deserializable.*rename'
rg -n -C3 --type=rust '\bcompiler_options\b'
rg -n -C2 --type=rust '"baseUrl"|base_url'Length of output: 9285 I want to double-check how 🏁 Script executed: #!/bin/bash
# Inspect TsConfigJson derive and serde attributes
rg -n '#\[derive' crates/biome_package/src/node_js_package/tsconfig_json.rs
rg -n 'rename_all' -C2 --type=rust crates/biome_package/src/node_js_package/tsconfig_json.rs
rg -n 'serde' -C2 --type=rust crates/biome_package/src/node_js_package/tsconfig_json.rsLength of output: 458 Let’s peek at the top of tsconfig_json.rs to see which derive and import paths 🏁 Script executed: #!/bin/bash
sed -n '1,30p' crates/biome_package/src/node_js_package/tsconfig_json.rsLength of output: 1081 Explicitly rename The /// The base URL is normalised to an absolute path after parsing.
- pub base_url: Option<Utf8PathBuf>,
+ #[deserializable(rename = "baseUrl")]
+ pub base_url: Option<Utf8PathBuf>,📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| /// Path aliases. | ||||||||||||||||||||
| pub paths: Option<CompilerOptionsPathsMap>, | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// The actual base from where path aliases are resolved. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// The base URL is normalised to an absolute path. | ||||||||||||||||||||
| #[deserializable(skip)] | ||||||||||||||||||||
| paths_base: Utf8PathBuf, | ||||||||||||||||||||
| pub paths_base: Utf8PathBuf, | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// See: https://www.typescriptlang.org/tsconfig/#typeRoots | ||||||||||||||||||||
| #[deserializable(rename = "typeRoots")] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,9 +183,15 @@ fn resolve_module_with_package_json( | |
| ) -> Result<Utf8PathBuf, ResolveError> { | ||
| // `tsconfig.json` may only be found in directories containing a | ||
| // `package.json`, so this is the only place we need to attempt to use it. | ||
| let tsconfig = fs.read_tsconfig_json(&package_path.join("tsconfig.json")); | ||
| if let Some(path) = tsconfig.as_ref().ok().and_then(|ts_config| { | ||
| resolve_paths_mapping(specifier, ts_config, package_path, fs, options).ok() | ||
| let tsconfig = match &options.tsconfig { | ||
| DiscoverableManifest::Auto => fs | ||
| .read_tsconfig_json(&package_path.join("tsconfig.json")) | ||
| .map(Cow::Owned), | ||
| DiscoverableManifest::Explicit { manifest, .. } => Ok(Cow::Borrowed(*manifest)), | ||
| DiscoverableManifest::Off => Err(ResolveError::NotFound), | ||
| }; | ||
|
Comment on lines
-186
to
+192
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes a slightly unrelated oversight: If an explicit manifest was configured for This came to light as I added tests. |
||
| if let Some(path) = tsconfig.as_ref().ok().and_then(|tsconfig| { | ||
| resolve_paths_mapping(specifier, tsconfig, package_path, fs, options).ok() | ||
| }) { | ||
| return Ok(path); | ||
| } | ||
|
|
@@ -222,6 +228,17 @@ fn resolve_module_with_package_json( | |
| ); | ||
| } | ||
|
|
||
| if let Some(base_url) = tsconfig | ||
| .as_ref() | ||
| .ok() | ||
| .and_then(|tsconfig| tsconfig.compiler_options.base_url.as_ref()) | ||
| { | ||
| match resolve_relative_path(specifier, base_url, fs, options) { | ||
| Err(ResolveError::NotFound) => { /* continue below */ } | ||
| result => return result, | ||
| } | ||
| } | ||
|
|
||
| resolve_dependency(specifier, package_path, fs, options) | ||
| } | ||
|
|
||
|
|
@@ -337,12 +354,12 @@ fn resolve_paths_mapping( | |
|
|
||
| let resolve_specifier = |specifier: &str| { | ||
| if is_relative_specifier(specifier) { | ||
| let base_dir = match &tsconfig_json.compiler_options.base_url { | ||
| Some(base_url) => base_url.as_path(), | ||
| None => package_path, | ||
| }; | ||
|
|
||
| resolve_relative_path(specifier, base_dir, fs, options) | ||
| resolve_relative_path( | ||
| specifier, | ||
| &tsconfig_json.compiler_options.paths_base, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| fs, | ||
| options, | ||
| ) | ||
| } else { | ||
| resolve_dependency(specifier, package_path, fs, options) | ||
| } | ||
|
|
@@ -686,6 +703,7 @@ fn strip_query_and_fragment(specifier: &str) -> &str { | |
| } | ||
|
|
||
| /// Options to pass to the resolver. | ||
| #[derive(Clone)] | ||
| pub struct ResolveOptions<'a> { | ||
| /// If `true`, specifiers are assumed to be relative paths. Resolving them | ||
| /// as a package will still be attempted if resolving as a relative path | ||
|
|
@@ -898,7 +916,7 @@ impl<'a> ResolveOptions<'a> { | |
| /// `tsconfig.json` will be automatically discovered, but this enum allows to | ||
| /// turn them off completely, or to provide an explicit manifest to be used | ||
| /// instead. | ||
| #[derive(Debug, Default)] | ||
| #[derive(Clone, Debug, Default)] | ||
| pub enum DiscoverableManifest<T> { | ||
| #[default] | ||
| Auto, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "resolver_cases_7" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "baseUrl": "./src" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a paragraph that explains the example below? That's the format that we try with the docs. E.g. "Given the following file structure ..., when importing a
"foo"fromindex.ts, Biome will automatically pick upsrc/foo.ts"