Skip to content

leading slash of compileOptions.filename is stripped in output, breaking absolute paths #8343

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

Open
dominikg opened this issue Mar 1, 2023 · 1 comment

Comments

@dominikg
Copy link
Member

dominikg commented Mar 1, 2023

Describe the bug

The svelte compiler adds information about the compiled files filename to the output. While doing so, it strips cwd and a leading slash.

Stripping the leading slash breaks absolute paths that don't start with cwd as the result would be

home/user/some/path/to/Component.svelte

which is relative and does not exist within cwd (/home/user/elsewhere/)

This results in svelte-inspector not being able to open components referenced via absolute paths, which can happen in a monorepo setup, see vite discord: https://discord.com/channels/804011606160703521/1075471833442287677

Strip happens here: https://github.com/sveltejs/svelte/blob/master/src/compiler/compile/Component.ts#L144
Introduced here: #1499

My best guess for a fix is that it should only strip the slash if it has stripped cwd, leaving other absolute paths alone.

Reproduction

Not reproducible in repl as process isn't defined there

go to https://vite.new/svelte , inspect the site with the counter button, enter document.getElementsByTagName('button')[0].__svelte_meta.loc.file in console and note that the path does not start with a /

document.getElementsByTagName('button')[0].__svelte_meta.loc.file
'src/lib/Counter.svelte'

Logs

n/a

System Info

linux, svelte 3.55.1

Severity

annoyance

@dominikg
Copy link
Member Author

dominikg commented Mar 2, 2023

workaround for svelte-inspector via dynamicCompileOptions

// svelte.config.js
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';
const cwd = process.cwd();
/** @type {import('@sveltejs/kit').Config} */
const config = {
    preprocess: vitePreprocess(),
    vitePlugin: {
        experimental: {
            inspector: true,
            dynamicCompileOptions({filename}) {
                return {
                    filename: !filename.startsWith(cwd) ? `/${filename}` : filename
                }
            }
        }
    },

    kit: {
        adapter: adapter()
    }
};

export default config;

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

No branches or pull requests

1 participant