dotnet / dotnet/project-system

DependentUpon with msbuild expression for items in subfolder

Open
#7,722 4 comments 0 reactions 1 assignee Claimed by @drewnoakes View on GitHub
Feature-File-Nesting Triage-Investigate
Dominant language
C#
Stars
1k
Forks
415
PR merge metrics
No merged PRs in 30d

Description

## Visual Studio Version

Microsoft Visual Studio Enterprise 2022 Preview
Version 17.0.0 Preview 6.0
VisualStudio.17.Preview/17.0.0-pre.6.0+31815.197
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Enterprise

## Summary

I am trying to add a `DependentUpon` metadata for all files of a certain type to nest any .cs file under that item in solution explorer.

Suppose you want to nest all .cs files under files with the same name but .txt extension on the same location.

You can end up with the following csproj:

```xml


netstandard2.0



$([MSBuild]::ValueOrDefault('%(Identity)', '').Replace(".cs",".txt").Replace("\","/"))

```

This change will work the first time you set it on visual studio for all files, even the files inside a subdirectory of the project.

But as soon as you close and relaunch VS, the files in the subdirectory will not be nested anymore.

As soon as you change the `.Replace("\","/")` expression to `.Replace("/","\")`, the files will be nested again, until you close and reopen VS, and you can change the expression again in the other way and VS will nest them again.

## Steps to Reproduce

1. Create a netstandard project
2. Add a file Class1.cs and Class1.txt on the project
3. Add a file Sub\Class2.cs and Sub\Class2.txt on the project
4. Add a file NotNested.cs
5. Change the .csproj to add the `...` expression from the Summary above
6. Observe VS: the file Class2.cs will be nested under Class2.txt, the file Class1.cs also under Class1.txt
7. Close and reopen VS: the file Class2 will not be nested anymore (Class1 will remain nested)
8. Change the .csproj replace expression from `.Replace("\","/")` to `.Replace("/","\")`
9. The file Class2.cs will be nested
10. Close and reopen VS: the file is not nested anymore
11. Rollback the change to the .csproj replace expression: the file will be nested
12. Close and reopen VS: the file is not nested anymore
13. Repeat :D

## Expected Behavior

The file should remain nested after VS is closed and reopened, or the file should never been nested in the first place.

## Actual Behavior

The file nesting for items in subfolder is not retained between VS instances.

## User Impact

I want to create a nuget package with a source generator which add a DependentUpon on all custom files for partial class from the user.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.