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

fix: use relative paths in sources for transformed source maps #12079

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -18,7 +18,6 @@ import {
createFilter,
ensureWatchedFile,
generateCodeFrame,
toUpperCaseDriveLetter,
} from '../utils'
import type { ResolvedConfig, ViteDevServer } from '..'
import type { Plugin } from '../plugin'
Expand Down Expand Up @@ -192,9 +191,6 @@ export async function transformWithEsbuild(
? JSON.parse(result.map)
: { mappings: '' }
}
if (Array.isArray(map.sources)) {
map.sources = map.sources.map((it) => toUpperCaseDriveLetter(it))
}
return {
...result,
map,
Expand Down
21 changes: 21 additions & 0 deletions packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -271,6 +271,27 @@ async function loadAndTransform(
if (map.mappings && !map.sourcesContent) {
await injectSourcesContent(map, mod.file, logger)
}
for (
let sourcesIndex = 0;
sourcesIndex < map.sources.length;
++sourcesIndex
) {
const sourcePath = map.sources[sourcesIndex]

// Rewrite sources to relative paths to give debuggers the chance
// to resolve and display them in a meaningful way (rather than
// with absolute paths).
if (
sourcePath &&
path.isAbsolute(sourcePath) &&
path.isAbsolute(mod.file)
) {
map.sources[sourcesIndex] = path.relative(
path.dirname(mod.file),
sourcePath,
)
Comment on lines +289 to +292
Copy link
Member

Choose a reason for hiding this comment

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

@sapphi-red just checking here that this path.relative call won't be affected by issues like the one you were worried about with paths in different drives on windows that can't be expressed as relative paths. I assume the path would be returned as absolute in this case. To be honest, maybe we should document having Vite work across hard drives as out of scope if this will increase complexity for everyone else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Windows also has this concept of relative paths with drive letters, which I think would also break vite.

I suppose instead of using path.relative here, we could also do string gymnastics ourselves, but that likely would be even worse / unpredictable.

Copy link
Member

Choose a reason for hiding this comment

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

I assume the path would be returned as absolute in this case.

Yes, you're correct. I think we can ignore the cross drive usage as it doesn't work in many places already at least for now.

}
}
}

const result =
Expand Down
4 changes: 0 additions & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -909,10 +909,6 @@ export function arraify<T>(target: T | T[]): T[] {
return Array.isArray(target) ? target : [target]
}

export function toUpperCaseDriveLetter(pathName: string): string {
return pathName.replace(/^\w:/, (letter) => letter.toUpperCase())
}

// Taken from https://stackoverflow.com/a/36328890
export const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g
export const singlelineCommentsRE = /\/\/.*/g
Expand Down
4 changes: 2 additions & 2 deletions playground/css-sourcemap/__tests__/css-sourcemap.spec.ts
Expand Up @@ -70,8 +70,8 @@ describe.runIf(isServe)('serve', () => {
{
"mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ",
"sources": [
"/root/be-imported.css",
"/root/linked-with-import.css",
"be-imported.css",
"linked-with-import.css",
],
"sourcesContent": [
".be-imported {
Expand Down
2 changes: 1 addition & 1 deletion playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Expand Up @@ -24,7 +24,7 @@ if (!isBuild) {
{
"mappings": "AAAO,aAAM,MAAM;",
"sources": [
"/root/bar.ts",
"bar.ts",
],
"sourcesContent": [
"export const bar = 'bar'
Expand Down