Skip to content
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

Unrequested edits applied to source code when importing existing files #72579

Closed
vsfeedback opened this issue Mar 18, 2024 · 6 comments
Closed

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work]


When adding existing source code files from outside the current solution/project, Visual Studio applies unrequested changes/edits to the imported (new copy) source code. Currently, seems to impact .cs files, unsure if other file types are impacted.

Examples of such changes/edits include:

  • Removal of paranthesis : if statements, inline conditions

For example: if ((a == b) && (! foo())) will become if (a == b && !foo())

  • Re-indentation of code

Code that is sub-indented (eg: parameters in a signature or function call, object construction, etc) will be re-indented.

While the changes do not cause any bugs or changes in functionality as far as I can tell and it seems to be applying some default "code stylesheet", this is not a "welcome" feature/bug! And it needs to be fixed.

I am setting a severity of 2 to this because this destroys:
(a) Code readability and maintainability
(b) Coding standards at our end

The problem has been around for a while. I am filing this only now because I just had to go through a 1000+ line .cs file to fix the changes!!!


Original Comments

Feedback Bot on 2/18/2024, 00:51 AM:

(private comment, text removed)

Sam Harwell [MSFT] on 2/20/2024, 04:12 PM:

(private comment, text removed)

Sujay V. Sarma on 3/2/2024, 02:36 AM:

(private comment, text removed)

Feedback Bot on 2/27/2024, 11:48 PM:

(private comment, text removed)

Sujay V. Sarma on 3/16/2024, 01:10 AM:

(private comment, text removed)

Feedback Bot on 3/18/2024, 09:06 AM:

(private comment, text removed)


Original Solutions

(no solutions)

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 18, 2024
@sharwell
Copy link
Member

Based on feedback from the customer, my best guess here is Code Cleanup on Save might be getting applied automatically to files added to a project with Add → Add Existing Item.

@ToddGrun
Copy link
Contributor

I'm unable to repro locally even with Code Cleanup on Save turned on.

@ghost
Copy link

ghost commented Mar 29, 2024

Hello,
Am the OP for this issue. This is consistent in our environments. What do you need to help you investigate this and bring a solution?

@CyrusNajmabadi
Copy link
Member

@sujayvsarma can you supply a repro case?

@ghost
Copy link

ghost commented Mar 31, 2024

@CyrusNajmabadi (Hi again!)

Repro steps:

  1. Create a c# project of any type. Let's use a Class Library for simplicity's sake.
  2. In Class1.cs, declare a dummy function foo and add the problematic lines of code.
  3. Open another instance of VS, create another c# project of any type. For simplicity, let's use Class Library again.
  4. Using any of the below methods, import Class1.cs from the previous project into the new one:
    • Add > Existing Item..., Browse to file and import it.
    • VS-to-VS copy/paste
    • VS-to-VS drag/drop

You will notice that the imported instance of Class1.cs is now "changed". The original instance of the file remains untouched.

Code that is typically changed in such a way:

  1. Parenthesis around conditions:
int a = 1, b = 2;
bool equal = ((a == b) || (a >= 1));

will become:

int a = 1, b = 2;
bool equal = a == b || a >= 1;
if ((a >= 1) && (b == 2)) {
  bool weird = true;
}

will become:

if (a >= 1 && b == 2) {
  bool weird = true;
}

and so on...

  1. If you have specifically indented code, then it will be re-indented as per whatever the OOTB stylesheet thinks is correct. For example, it is typical in function calls or structure inits to use offset-indentation thus:
bool result = foo(this,
                that,
                somethingelse
              );

The bug we are talking about will cause this to become:

bool result = foo(this,that,somethingelse);

Until now, in the cases we have come across, the meaning and intent of the statement(s) have not been modified by such changes (which would result in functional bugs). However, it is not inconceivable to wonder if that might happen, in some specific un-approved circumstances and nobody finds about it unless something really bad happens in Prod.

@CyrusNajmabadi
Copy link
Member

Closing out. Fixed with #76019

@dotnet-policy-service dotnet-policy-service bot removed the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants