Skip to content

fix: wrap module script content in block scope to prevent false redeclaration errors - #308027

Merged
Dmitriy Vasyura (dmitrivMS) merged 11 commits into
microsoft:mainfrom
yogeshwaran-c:fix/html-module-script-scope
Aug 1, 2026
Merged

fix: wrap module script content in block scope to prevent false redeclaration errors#308027
Dmitriy Vasyura (dmitrivMS) merged 11 commits into
microsoft:mainfrom
yogeshwaran-c:fix/html-module-script-scope

Conversation

@yogeshwaran-c

@yogeshwaran-c Yogeshwaran C (yogeshwaran-c) commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes [html] Vscode detect wrong scope for script with type=module #229357
  • When an HTML file contains classic and module scripts that declare variables with the same name, VS Code incorrectly reports "Cannot redeclare block-scoped variable" because all script contents are concatenated into a single virtual JavaScript document for validation.
  • Detects <script type="module"> case-insensitively and validates classic scripts together while validating each module script as a separate ES module source document.
  • Preserves source offsets for diagnostics and keeps the aggregate embedded document used by other JavaScript language features.
  • Adds diagnostic-level coverage for classic/module and module/module isolation, var declarations, classic-script redeclarations, module syntax, top-level await, and diagnostic range mapping.

Test plan

  • Classic and module scripts can declare the same name without a false redeclaration error.
  • Multiple module scripts can declare the same name, including var, without a false redeclaration error.
  • Regular scripts still report duplicate block-scoped declarations.
  • Module syntax (import.meta, export) and top-level await validate without syntax errors.
  • Module diagnostics map back to the correct HTML source ranges.
  • HTML language server test suite passes (71 tests).

…icrosoft#229357)

When multiple <script> tags exist in an HTML file, the HTML language
server concatenates their content into a single virtual JavaScript
document for validation. This causes false "Cannot redeclare
block-scoped variable" errors when <script type="module"> tags declare
variables with the same name as regular <script> tags, since module
scripts should have their own scope per the HTML spec.

Fix by wrapping <script type="module"> content in block scope delimiters
({ ... }) in the virtual document, preventing variable name collisions
between module scripts and regular scripts.
Copilot AI review requested due to automatic review settings July 29, 2026 00:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Attempts to isolate HTML module scripts in embedded JavaScript validation.

Changes:

  • Tracks module script regions.
  • Wraps module contents in braces.
  • Adds embedded-document tests.
Show a summary per file
File Description
embeddedSupport.ts Detects and wraps module scripts.
embedded.test.ts Tests module detection and generated content.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Medium

Comment thread extensions/html-language-features/server/src/modes/embeddedSupport.ts Outdated
Comment thread extensions/html-language-features/server/src/test/embedded.test.ts Outdated
@dmitrivMS

Copy link
Copy Markdown
Contributor

Addressed the review feedback in 81a4f47.

The brace-based approach has been replaced with validation-specific embedded documents: classic scripts are validated together, while each <script type="module"> region is validated as a separate ES module source file. This avoids false cross-module redeclarations, also isolates var, and preserves valid module-only syntax such as export, import.meta, and top-level await. The original aggregate embedded document remains unchanged for completion, hover, rename, and other editor features, and module diagnostic offsets remain aligned with the HTML document.

The tests now exercise diagnostic behavior instead of only checking generated braces. Coverage includes classic/module and module/module duplicate names, var isolation, real classic-script redeclarations, valid module syntax, top-level await, diagnostic range mapping, and case-insensitive module type detection.

Validation: HTML language server compilation succeeded, all 70 server tests passed, and hygiene passed.

@dmitrivMS

Copy link
Copy Markdown
Contributor

Added two focused regression checks in 9920ae5: classic let versus module var isolation, and multiline module diagnostic line/column mapping. Compilation, the focused validation suite, and hygiene pass.

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) enabled auto-merge (squash) July 29, 2026 06:49
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) enabled auto-merge (squash) July 31, 2026 23:55
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 6dc67ef into microsoft:main Aug 1, 2026
29 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.132.0 milestone Aug 1, 2026
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

Successfully merging this pull request may close these issues.

[html] Vscode detect wrong scope for script with type=module

9 participants