"Generate new type" creates nested namespace when file namespace does not match root namespace
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
**Version Used**: VS 18.2.0
**Steps to Reproduce**:
1. Create a project whose name is not "Foo".
2. Paste the following file.
3. On the `D` symbol, open the lightbulb menu and go to "Generate type 'D'" > "Generate new type..."
4. In the dialog, make sure that "Add to existing file" > "Current file" is selected. Confirm the dialog.
```csharp
namespace Foo;
class C(D _);
```
**Expected Behavior**:
The new type is placed in the same file and namespace. This is what the "Generate class '<name>'" refactoring does.
```csharp
namespace Foo;
class C(D _);
class D
{
}
```
**Actual Behavior**:
The new type is generated in the root namespace. In the case of file-scoped namespaces, this leads to syntax errors.
```csharp
using Roslyntest; // CS0246 The type or namespace name 'Roslyntest' could not be found (are you missing a using directive or an assembly reference?)
namespace Foo;
class C(D _); // CS0246 The type or namespace name 'D' could not be found (are you missing a using directive or an assembly reference?)
namespace Roslyntest // CS8955 Source file can not contain both file-scoped and normal namespace declarations.
{
class D
{
}
}
```
If block namespaces are used, there are no syntax errors, but the new type is still in a different namespace.
```csharp
using Roslyntest;
namespace Foo
{
class C(D _);
}
namespace Roslyntest
{
class D
{
}
}
```
Contributor guide
Research direction
Reproduce the issue using the file-scoped and block namespace examples, then start from the "Generate new type..." refactoring entry point and compare it with the "Generate class" refactoring. Done means the new type is added to the current file's existing namespace without introducing a conflicting namespace declaration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100