Skip to content

fix: ensure compiler state is reset before compilation #16396

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

Merged
merged 1 commit into from
Jul 16, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-terms-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure compiler state is reset before compilation
6 changes: 3 additions & 3 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export { default as preprocess } from './preprocess/index.js';
*/
export function compile(source, options) {
source = remove_bom(source);
state.reset_warnings(options.warningFilter);
state.reset({ warning: options.warningFilter, filename: options.filename });
const validated = validate_component_options(options, '');

let parsed = _parse(source);
Expand Down Expand Up @@ -63,7 +63,7 @@ export function compile(source, options) {
*/
export function compileModule(source, options) {
source = remove_bom(source);
state.reset_warnings(options.warningFilter);
state.reset({ warning: options.warningFilter, filename: options.filename });
const validated = validate_module_options(options, '');

const analysis = analyze_module(source, validated);
Expand Down Expand Up @@ -111,7 +111,7 @@ export function compileModule(source, options) {
*/
export function parse(source, { modern, loose } = {}) {
source = remove_bom(source);
state.reset_warnings(() => false);
state.reset({ warning: () => false, filename: undefined });

const ast = _parse(source, loose);
return to_public_ast(source, ast, modern);
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { parse } from '../phases/1-parse/index.js';
import { regex_valid_component_name } from '../phases/1-parse/state/element.js';
import { analyze_component } from '../phases/2-analyze/index.js';
import { get_rune } from '../phases/scope.js';
import { reset, reset_warnings } from '../state.js';
import { reset, UNKNOWN_FILENAME } from '../state.js';
import {
extract_identifiers,
extract_all_identifiers_from_expression,
Expand Down Expand Up @@ -134,7 +134,7 @@ export function migrate(source, { filename, use_ts } = {}) {
return start + style_placeholder + end;
});

reset_warnings(() => false);
reset({ warning: () => false, filename });

let parsed = parse(source);

Expand All @@ -145,7 +145,7 @@ export function migrate(source, { filename, use_ts } = {}) {
...validate_component_options({}, ''),
...parsed_options,
customElementOptions,
filename: filename ?? '(unknown)',
filename: filename ?? UNKNOWN_FILENAME,
experimental: {
async: true
}
Expand Down
8 changes: 3 additions & 5 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ export function analyze_module(source, options) {
classes: new Map()
};

state.reset({
state.adjust({
dev: options.dev,
filename: options.filename,
rootDir: options.rootDir,
runes: true
});
Expand Down Expand Up @@ -531,12 +530,11 @@ export function analyze_component(root, source, options) {
async_deriveds: new Set()
};

state.reset({
state.adjust({
component_name: analysis.name,
dev: options.dev,
filename: options.filename,
rootDir: options.rootDir,
runes: true
runes
});

if (!runes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { visit_component } from './shared/component.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { filename } from '../../../state.js';
import { filename, UNKNOWN_FILENAME } from '../../../state.js';

/**
* @param {AST.SvelteSelf} node
Expand All @@ -23,9 +23,9 @@ export function SvelteSelf(node, context) {
}

if (context.state.analysis.runes) {
const name = filename === '(unknown)' ? 'Self' : context.state.analysis.name;
const name = filename === UNKNOWN_FILENAME ? 'Self' : context.state.analysis.name;
const basename =
filename === '(unknown)'
filename === UNKNOWN_FILENAME
? 'Self.svelte'
: /** @type {string} */ (filename.split(/[/\\]/).pop());

Expand Down
37 changes: 24 additions & 13 deletions packages/svelte/src/compiler/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export let warnings = [];
*/
export let filename;

/**
* This is the fallback used when no filename is specified.
*/
export const UNKNOWN_FILENAME = '(unknown)';

/**
* The name of the component that is used in the `export default function ...` statement.
*/
Expand Down Expand Up @@ -80,15 +85,6 @@ export function pop_ignore() {
ignore_stack.pop();
}

/**
*
* @param {(warning: Warning) => boolean} fn
*/
export function reset_warnings(fn = () => true) {
warning_filter = fn;
warnings = [];
}

/**
* @param {AST.SvelteNode | NodeLike} node
* @param {import('../constants.js').IGNORABLE_RUNTIME_WARNINGS[number]} code
Expand All @@ -99,21 +95,36 @@ export function is_ignored(node, code) {
}

/**
* Call this to reset the compiler state. Should be called before each compilation.
* @param {{ warning?: (warning: Warning) => boolean; filename: string | undefined }} state
*/
export function reset(state) {
dev = false;
runes = false;
component_name = UNKNOWN_FILENAME;
source = '';
locator = () => undefined;
filename = (state.filename ?? UNKNOWN_FILENAME).replace(/\\/g, '/');
warning_filter = state.warning ?? (() => true);
warnings = [];
}

/**
* Adjust the compiler state based on the provided state object.
* Call this after parsing and basic analysis happened.
* @param {{
* dev: boolean;
* filename: string;
* component_name?: string;
* rootDir?: string;
* runes: boolean;
* }} state
*/
export function reset(state) {
export function adjust(state) {
const root_dir = state.rootDir?.replace(/\\/g, '/');
filename = state.filename.replace(/\\/g, '/');

dev = state.dev;
runes = state.runes;
component_name = state.component_name ?? '(unknown)';
component_name = state.component_name ?? UNKNOWN_FILENAME;

if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
// make filename relative to rootDir
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/utils/compile_diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class CompileDiagnostic {
this.code = code;
this.message = message;

if (state.filename) {
if (state.filename !== state.UNKNOWN_FILENAME) {
this.filename = state.filename;
}

Expand Down
14 changes: 13 additions & 1 deletion packages/svelte/tests/compiler-errors/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'node:fs';
import { assert, expect } from 'vitest';
import { assert, expect, it } from 'vitest';
import { compile, compileModule, type CompileError } from 'svelte/compiler';
import { suite, type BaseTest } from '../suite';
import { read_file } from '../helpers.js';
Expand Down Expand Up @@ -78,3 +78,15 @@ const { test, run } = suite<CompilerErrorTest>((config, cwd) => {
export { test };

await run(__dirname);

it('resets the compiler state including filename', () => {
// start with something that succeeds
compile('<div>hello</div>', { filename: 'foo.svelte' });
// then try something that fails in the parsing stage
try {
compile('<p>hello<div>invalid</p>', { filename: 'bar.svelte' });
expect.fail('Expected an error');
} catch (e: any) {
expect(e.toString()).toContain('bar.svelte');
}
});
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy