-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Remove baseUrl from tsconfig #505
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
refactor: Remove baseUrl from tsconfig #505
Conversation
ENG-637 Remove baseUrl from tsconfig
base URL is no longer necessary for paths and risks adding bare paths |
🦋 Changeset detectedLatest commit: 807197a The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughThis change set removes the Changes
Sequence Diagram(s)sequenceDiagram
participant TSConfig as tsconfig.json
participant Compiler as TypeScript Compiler
participant Generator as Code Generator
participant Renderer as Path Map Renderer
TSConfig->>Compiler: Provide config (without baseUrl, with "@src/*": "./src/*")
Generator->>Renderer: Call generatePathMapEntries(baseUrl, paths)
Renderer->>Renderer: Use baseUrl ?? '.' for path resolution
Renderer-->>Generator: Return resolved path entries
Compiler-->>Application: Resolve imports using updated paths
sequenceDiagram
participant ESLint as ESLint
participant Developer as Developer
Developer->>ESLint: Write/import using relative package path
ESLint-->>Developer: Error ('import-x/no-relative-packages')
sequenceDiagram
participant Test as Unit Test
participant Renderer as Path Map Renderer
Test->>Renderer: Call generatePathMapEntries(undefined, paths)
Renderer->>Renderer: Use '.' as default base path
Renderer-->>Test: Return correct path mappings
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Lite
📒 Files selected for processing (24)
.changeset/olive-guests-argue.md(1 hunks)packages/code-morph/tsconfig.json(1 hunks)packages/core-generators/src/generators/node/typescript/typescript.generator.ts(3 hunks)packages/core-generators/src/generators/node/typescript/typescript.generator.unit.test.ts(0 hunks)packages/core-generators/src/renderers/typescript/imports/ts-path-maps.ts(2 hunks)packages/core-generators/src/renderers/typescript/imports/ts-path-maps.unit.test.ts(1 hunks)packages/core-generators/tsconfig.json(1 hunks)packages/create-project/tsconfig.json(1 hunks)packages/fastify-generators/src/generators/core/fastify/setup-fastify-typescript.ts(0 hunks)packages/fastify-generators/tsconfig.json(1 hunks)packages/project-builder-cli/tsconfig.app.json(1 hunks)packages/project-builder-cli/tsconfig.e2e.json(1 hunks)packages/project-builder-lib/tsconfig.json(1 hunks)packages/project-builder-server/src/plugins/node-plugin-store.ts(1 hunks)packages/project-builder-server/tsconfig.json(1 hunks)packages/project-builder-test/tsconfig.json(1 hunks)packages/project-builder-web/tsconfig.json(1 hunks)packages/react-generators/src/generators/core/react-typescript/react-typescript.generator.ts(0 hunks)packages/react-generators/tsconfig.json(1 hunks)packages/sync/tsconfig.json(1 hunks)packages/tools/eslint-configs/typescript.js(1 hunks)packages/ui-components/tsconfig.json(1 hunks)packages/utils/tsconfig.json(1 hunks)plugins/baseplate-plugin-storage/tsconfig.json(1 hunks)
💤 Files with no reviewable changes (3)
- packages/react-generators/src/generators/core/react-typescript/react-typescript.generator.ts
- packages/core-generators/src/generators/node/typescript/typescript.generator.unit.test.ts
- packages/fastify-generators/src/generators/core/fastify/setup-fastify-typescript.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/core-generators/src/renderers/typescript/imports/ts-path-maps.unit.test.ts (1)
packages/core-generators/src/renderers/typescript/imports/ts-path-maps.ts (1)
generatePathMapEntries(12-27)
🔇 Additional comments (25)
packages/project-builder-cli/tsconfig.e2e.json (2)
5-7: Consistent Path Alias UpdateThe
@src/*mapping has been correctly updated to a relative path ("./src/*"), which aligns with the project-wide removal ofbaseUrland maintains import resolution consistency in this E2E config.
2-2: VerifybaseUrlin Extended ConfigurationTypeScript only applies
pathsmappings when abaseUrlis set. Since this file inherits from@halfdomelabs/tools/tsconfig.node.cli.jsonand no longer declares its ownbaseUrl, please confirm that the extended config still defines a validbaseUrl. Otherwise, these path aliases won’t take effect.plugins/baseplate-plugin-storage/tsconfig.json (2)
2-3: Ensure removal ofbaseUrlis effective.This config extends
@halfdomelabs/tools/tsconfig.vite.lib.json. Verify that the extended config does not reintroduce abaseUrloption (which could override our removal). If it does, consider overriding it here (e.g.,"baseUrl": ".") or removing it upstream so thatbaseUrlis truly absent across the build.
7-7: Approve updated path mapping.Updating
"@src/*": ["./src/*"]to use an explicit relative path is correct and aligns with the removal ofbaseUrl. Ensure that all imports using@src/resolve correctly—e.g., by runningtscor leveragingtsc-aliasin your build pipeline.packages/code-morph/tsconfig.json (1)
6-6: Consistent update of path alias after removingbaseUrl.The alias
@src/*is now mapped to./src/*, explicitly relative to the TS config root. This aligns with the removal ofbaseUrland maintains correct module resolution.packages/ui-components/tsconfig.json (1)
6-6: Path alias adjusted to explicit relative path.Mapping
@src/*→./src/*ensures imports still resolve correctly without abaseUrl. Nice consistency with other packages.packages/fastify-generators/tsconfig.json (1)
6-6: Updated@src/*path mapping for removedbaseUrl.Explicitly prefixing with
./aligns with the TS defaultbaseUrl = "."and preserves correct alias resolution.packages/project-builder-web/tsconfig.json (1)
6-6: Normalized alias path following baseUrl removal.The change to
./src/*is correct given the default base directory, ensuring@src/*resolves under the workspace.packages/project-builder-test/tsconfig.json (1)
7-7: Confirmed explicit path mapping post-baseUrlremoval.Mapping
@src/*to./src/*is correctly scoped to the project root, restoring alias resolution in the absence ofbaseUrl.packages/react-generators/tsconfig.json (1)
6-6: Path mapping updated correctly to use explicit relative path.The path mapping for
@src/*has been properly updated to use an explicit relative path"./src/*"instead of (presumably)"src/*". This change aligns with the PR objective of removing thebaseUrloption while ensuring path mappings continue to work correctly.packages/project-builder-server/tsconfig.json (1)
7-7: Path mapping updated correctly to use explicit relative path.The path mapping for
@src/*has been properly updated to use an explicit relative path"./src/*"instead of (presumably)"src/*". This change aligns with the PR objective of removing thebaseUrloption while ensuring path mappings continue to work correctly.packages/project-builder-server/src/plugins/node-plugin-store.ts (1)
3-3: Type import aligned with new ESLint rule for package imports.The import of
PluginPlatformModulehas been updated to use the package's main entry point instead of a direct internal path. This aligns with the new ESLint rule to disallow relative package imports and is a good practice for maintaining clean dependency boundaries.packages/core-generators/src/renderers/typescript/imports/ts-path-maps.ts (2)
13-13: Function parameter updated to handle undefined baseUrl.The
baseUrlparameter type has been correctly updated tostring | undefinedto accommodate the removal of thebaseUrloption from TypeScript configurations. This change ensures the function can gracefully handle cases wherebaseUrlis not provided.
23-23: Path joining logic updated to handle undefined baseUrl.The path joining logic has been updated to use the nullish coalescing operator (
baseUrl ?? '.'), which properly defaults to'.'whenbaseUrlis undefined. This ensures that the path joining operation always has a valid base path, preventing potential errors whenbaseUrlis not provided.packages/core-generators/src/generators/node/typescript/typescript.generator.ts (3)
216-217: Improved conditional structure for path entry handlingThe conditional logic has been restructured to be more explicit with an
else ifand added anelsebranch to handle the case when neither condition is met. This is a good improvement for code clarity and completeness.Also applies to: 233-235
228-228: Added fallback for undefined baseUrlThe addition of the nullish coalescing operator
??ensures that a default value of'.'is used whenbaseUrlis undefined, which supports the removal ofbaseUrlfrom tsconfig files while maintaining compatibility.
419-419: Re-exported TypescriptCompilerOptions typeGood practice to re-export this type directly from the module, making it easier for consumers to access it without needing to know the internal structure.
packages/project-builder-lib/tsconfig.json (1)
7-7: Updated path mapping to use explicit relative pathThe path mapping has been updated to include a leading
./which explicitly denotes a relative path. This change is necessary after removing thebaseUrloption to ensure TypeScript correctly resolves imports.packages/sync/tsconfig.json (1)
7-7: Updated path mapping to use explicit relative pathThe path mapping has been updated to include a leading
./which explicitly denotes a relative path. This change is necessary after removing thebaseUrloption to ensure TypeScript correctly resolves imports..changeset/olive-guests-argue.md (1)
1-6: Clear changeset documentationThe changeset properly documents the patch updates to the affected packages and clearly explains the purpose of removing the
baseUrlconfiguration. This is helpful for tracking the change and understanding its motivation.packages/tools/eslint-configs/typescript.js (1)
147-149: Good addition to enforce proper package imports.Adding the
import-x/no-relative-packagesrule is a good complement to the removal ofbaseUrl. This rule prevents importing from other packages using relative paths (like'../other-package/foo'), which encourages treating each package as an independent unit and makes the codebase more maintainable.packages/project-builder-cli/tsconfig.app.json (1)
6-6: Correct update to path mapping.The path mapping for
@src/*has been properly updated to use an explicit relative path (./src/*), which is necessary after removing thebaseUrlcompiler option. This change ensures that the path alias continues to work correctly.packages/utils/tsconfig.json (1)
7-7: Correct update to path mapping.The path mapping for
@src/*has been properly updated to use an explicit relative path (./src/*), which is necessary after removing thebaseUrlcompiler option. This change ensures that the path alias continues to work correctly.packages/core-generators/tsconfig.json (1)
7-7: Correct update to path mapping.The path mapping for
@src/*has been properly updated to use an explicit relative path (./src/*), which is necessary after removing thebaseUrlcompiler option. This change ensures that the path alias continues to work correctly.packages/core-generators/src/renderers/typescript/imports/ts-path-maps.unit.test.ts (1)
29-41: LGTM! Well-structured test for handling undefined baseUrl.This new test case properly verifies the updated behavior of the
generatePathMapEntriesfunction whenbaseUrlis undefined. It follows the arrange-act-assert pattern and ensures that path mappings work correctly without a baseUrl, which is critical for the project-wide removal of thebaseUrloption.The test confirms that the implementation correctly uses the fallback value (
.) whenbaseUrlis undefined, as implemented in thets-path-maps.jsfile with the nullish coalescing operator:baseUrl ?? '.'.
| "baseUrl": ".", | ||
| "paths": { | ||
| "@src/*": ["src/*"] | ||
| "@src/*": ["./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.
💡 Verification agent
🧩 Analysis chain
Verify paths mapping without an explicit baseUrl.
TypeScript’s paths feature generally requires a baseUrl to resolve non-relative imports. Since you’ve removed baseUrl here, please confirm that the extended config (@halfdomelabs/tools/tsconfig.node.cli.json) still defines it; otherwise, you may encounter TS5028 errors at build time. If it isn’t defined downstream, consider reintroducing:
"compilerOptions": {
+ "baseUrl": ".",
"outDir": "dist",
"paths": {
"@src/*": ["./src/*"]
}
}to ensure module aliases are resolved correctly.
To verify, you can run:
🏁 Script executed:
#!/bin/bash
# Check for `baseUrl` in the extended tsconfig
rg -n '"baseUrl"' -g 'tsconfig.node.cli.json'Length of output: 45
🏁 Script executed:
#!/bin/bash
# Locate the extended tsconfig file
file=$(find . -type f -name 'tsconfig.node.cli.json' | head -n1)
if [ -z "$file" ]; then
echo "tsconfig.node.cli.json not found"
exit 1
fi
echo "Found tsconfig at: $file"
# Check for a baseUrl definition
echo "Checking for baseUrl in $file:"
rg -n '"baseUrl"' "$file" || echo "No baseUrl defined in $file"Length of output: 617
Add a baseUrl to enable your paths mapping
We’ve confirmed that packages/tools/tsconfig.node.cli.json does not define a baseUrl. Since TypeScript requires baseUrl whenever you use paths, you will encounter TS5028 errors at build time.
Please update packages/create-project/tsconfig.json to include:
"compilerOptions": {
+ "baseUrl": ".",
"outDir": "dist",
"paths": {
"@src/*": ["./src/*"]
}
}This ensures @src/* resolves correctly.
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.
That is actually inaccurate. You do not need a baseUrl to enable paths mapping since Typescript 4.1. The point of this PR is to remove the baseURL to ensure that we don't allow bare paths in imports e.g. node_modules/random_package.
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.
🧩 Analysis chain
@kingston You're absolutely right, and I apologize for the incorrect information. TypeScript 4.1+ indeed supports paths mapping without baseUrl when using relative paths (with "./").
The approach in your PR makes perfect sense - removing baseUrl while updating path mappings to use explicit relative paths ("./src/*") accomplishes your goal of preventing bare path imports while maintaining the alias functionality.
Thank you for the clarification about the purpose of this change!
🌐 Web query:
TypeScript 4.1 paths mapping without baseUrl
💡 Result:
TypeScript 4.1: paths Mapping Without baseUrl
Overview
Starting with TypeScript 4.1, you can use the paths option in your tsconfig.json without specifying a baseUrl. This change removed a long-standing restriction and simplified path mapping for TypeScript projects[2][4][5].
Previous Behavior
Prior to TypeScript 4.1, if you tried to use the paths option without a baseUrl, the compiler would throw an error:
"Option 'paths' cannot be used without specifying '--baseUrl' option"[1][3].
This meant that any path mapping required you to set baseUrl, usually to either "./" or another root-relative directory.
What's Changed in TypeScript 4.1?
- No More
baseUrlRequirement:
With TypeScript 4.1 and later, you can specifypathsmappings without definingbaseUrlin yourtsconfig.json. - Resolution is Relative to the Project Root:
IfbaseUrlis not set, the project directory (wheretsconfig.jsonis located) is used as the base for resolving paths[1][4]. - Better Editor Experience:
This update also improved the auto-import suggestions in editors like VS Code, aligning import paths more closely with your intended mappings[1].
Example Configuration
{
"compilerOptions": {
"paths": {
"@utils/*": ["src/utils/*"],
"@components/*": ["src/components/*"]
}
}
}With this configuration (and no baseUrl), @utils/foo will resolve to src/utils/foo relative to the project root[4].
References in Official Documentation
- The TypeScript 4.1 release notes confirm that
baseUrlis no longer required forpaths[2][5]. - The TSConfig reference also documents this change[4].
Summary Table
| TypeScript Version | baseUrl Required for paths? |
Behavior |
|---|---|---|
| ≤ 4.0 | Yes | Error if omitted |
| 4.1+ | No | Resolves from project root |
In Practice
If you want to use paths mappings as of TypeScript 4.1 or later, you may safely omit baseUrl from your configuration. TypeScript will resolve the mappings from the directory containing your tsconfig.json, improving both developer experience and configuration flexibility[1][2][4][5].
Citations:
- 1: Proposal: Allow
pathscompilerOption withoutbaseUrlmicrosoft/TypeScript#31869 - 2: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html
- 3: https://youtrack.jetbrains.com/issue/WEB-50464/TypeScript-Cannot-use-paths-without-baseUrl
- 4: https://www.typescriptlang.org/tsconfig/baseUrl.html
- 5: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/
- 6: https://www.typescriptlang.org/tsconfig/
- 7: Cannot find definitions with baseUrl microsoft/TypeScript#50879
@kingston Thank you for correcting me! You're absolutely right - since TypeScript 4.1, path mappings can be used without specifying baseUrl. The web search confirms that when baseUrl is omitted, TypeScript resolves paths relative to the project directory (where tsconfig.json is located).
Your approach of removing baseUrl while updating paths to use explicit relative paths ("./src/*") perfectly achieves the goal of preventing bare path imports like node_modules/random_package while maintaining the path alias functionality.
I appreciate you sharing this knowledge about TypeScript's behavior!
✏️ Learnings added
Learnt from: kingston
PR: halfdomelabs/baseplate#505
File: packages/create-project/tsconfig.json:6-6
Timestamp: 2025-04-21T06:32:22.453Z
Learning: Since TypeScript 4.1, baseUrl is not required for paths mapping when using explicit relative paths (with "./"). Removing baseUrl from tsconfig.json while updating paths to use relative paths (e.g., changing "@src/*": ["src/*"] to "@src/*": ["./src/*"]) prevents bare path imports from node_modules while maintaining path alias functionality.
Learnt from: kingston
PR: halfdomelabs/baseplate#505
File: packages/create-project/tsconfig.json:6-6
Timestamp: 2025-04-21T06:32:22.453Z
Learning: Since TypeScript 4.1, baseUrl is not required for paths mapping in tsconfig.json. Removing baseUrl and using explicit relative paths with "./" prefix (e.g., changing "@src/*": ["src/*"] to "@src/*": ["./src/*"]) prevents bare path imports from node_modules while maintaining path alias functionality.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
Summary by CodeRabbit
Refactor
baseUrlcompiler option from multiple TypeScript configurations across the project.@src/*alias to use explicit relative paths for improved consistency.baseUrlis undefined.Style
Tests
Chores