dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.98k stars 4.03k forks source link

Code formatting issue with standalone C# files #30841

Open jbevain opened 5 years ago

jbevain commented 5 years ago

Version Used:

Microsoft Visual Studio Community 2017 
Version 15.8.8
VisualStudio.15.Release/15.8.8+28010.2048
Microsoft .NET Framework
Version 4.7.03056

C# Tools   2.9.0-beta8-63208-01

Steps to Reproduce:

  1. Set Visual Studio to perform additional cleanups during formatting.

setting

  1. Create a new standalone file test.cs.
  2. Copy the following into test.cs
using System;

using UnityEngine;
using UnityEditor;

class Camera : MonoBehaviour {

    public void Start() {
    }

    public void Update() {
    }
}
  1. Open the test.cs file in Visual Studio as a standalone C# file outside of a project.

Expected Behavior:

File is formatted to my settings, for example:

using System;

using UnityEngine;
using UnityEditor;

class Camera : MonoBehaviour
{
    public void Start()
    {
    }

    public void Update()
    {
    }
}

Actual Behavior:

File is formatted to my settings, but removes the using statements. For instance:

class Camera : MonoBehaviour
{
    public void Start()
    {
    }

    public void Update()
    {
    }
}

Opening a standalone C# file outside of a project prevents us to know all the references the file needs. If we're reformatting a standalone C# file we probably shouldn't perform cleanups that are potentially destructive.

sharwell commented 5 years ago

@jaredpar This is a compiler bug: the compiler should not report CS8019 when one or more identifiers fails to bind during semantic analysis.

jaredpar commented 5 years ago

CC @gafter pretty sure this has been discussed before and there are good reasons for why we do this. Or possibly bad ones we just can't reasonably fix.

The problem is not going to be limited to identifier binding errors. If the compiler emits pretty much any other error you should not be removing unused namespaces. Any fix to the error is likely to reveal other namespaces that are in use.