Skip to content

Resolve using declared before the file namespace from the global namespace - #11552

Open
timotheeguerin wants to merge 3 commits into
microsoft:mainfrom
timotheeguerin:file-ns-using-conflict
Open

Resolve using declared before the file namespace from the global namespace#11552
timotheeguerin wants to merge 3 commits into
microsoft:mainfrom
timotheeguerin:file-ns-using-conflict

Conversation

@timotheeguerin

@timotheeguerin timotheeguerin commented Aug 5, 2026

Copy link
Copy Markdown
Member

Writing a using above a file-level namespace silently changed what it meant. Because the using was resolved as if it lived inside the file namespace, this failed:

using TypeSpec.Http;
namespace _Specs_.TypeSpec.Foo;

error invalid-ref: Namespace _Specs_.TypeSpec doesn't have member Http

TypeSpec bound to _Specs_.TypeSpec instead of the global TypeSpec, even though the using is written before the namespace declaration. Switching to a block namespace made it work, which is confusing.

Our namespace system is modelled on C#, and C# does not have this problem: a using before a file-scoped namespace is compilation-unit level and resolves from the global namespace; a using after it lives inside the namespace and resolves relatively. This PR adopts the same rule.

using Common.Models;              // resolves the global `Common`
namespace MyOrg.Common.Service;
namespace MyOrg.Service;
using Models;                     // unchanged: resolves `MyOrg.Models`

Breaking change

A relative name in a using written above the file namespace now needs to be fully qualified, otherwise it reports Unknown identifier.

Follow-ups

Fixes #1840

…m the global namespace

A `using` written above a blockless namespace declaration was resolved as if
it were inside the file namespace, so `using TypeSpec.Http;` in a file with
`namespace _Specs_.TypeSpec.Foo;` bound `TypeSpec` to `_Specs_.TypeSpec`.

Match C#: usings declared before the file namespace resolve from the global
namespace, while those declared after it (or inside a namespace block) keep
resolving relative to that namespace.

Fixes microsoft#1840
@timotheeguerin timotheeguerin added the int:azure-specs Run integration tests against azure-rest-api-specs label Aug 5, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added compiler:core Issues for @typespec/compiler meta:website TypeSpec.io updates labels Aug 5, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/compiler@11552

commit: a12422e

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • βœ… @typespec/compiler
Show changes

@typespec/compiler - breaking ✏️

using statements declared before a file-level(blockless) namespace are now resolved from the global namespace instead of the file namespace, matching C#.,> ,> tsp,> using TypeSpec.Http; // Now resolves the global `TypeSpec` namespace instead of `_Specs_.TypeSpec`,> namespace _Specs_.TypeSpec.Foo;,> ,> ,> A using declared after the file namespace, or inside a namespace block, is unchanged and still resolves relative to that namespace. Code relying on a relative name in a using written above the file namespace must now use the fully qualified name.,> ,> tsp,> namespace MyOrg.Service;,> using Models; // Still resolves to `MyOrg.Models`,>

@azure-sdk-automation

azure-sdk-automation Bot commented Aug 5, 2026

Copy link
Copy Markdown

You can try these changes here

πŸ› Playground 🌐 Website πŸ› VSCode Extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler int:azure-specs Run integration tests against azure-rest-api-specs meta:website TypeSpec.io updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

using is conflicting with file namespace

1 participant