msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

-targets command-line option doesn't work with solution file

Open ghogen opened this issue 2 years ago • 10 comments

Issue Description

-targets command-line option doesn't work when building a solution

Steps to Reproduce

MSBuild.exe -targets SomeSolution.sln

Expected Behavior

List the available targets in each project

Actual Behavior

Builds the solution

Versions & Configurations

Windows 10 MSBuild version 17.3.0-preview-22307-01+bfd80ab16 for .NET Framework

ghogen avatar Jun 10 '22 17:06 ghogen

In addition to "working on solutions at all", this should have a blurb about the dynamically-generated solution targets that delegate to all projects.

rainersigwald avatar Jun 15 '22 14:06 rainersigwald

In addition to "working on solutions at all", this should have a blurb about the dynamically-generated solution targets that delegate to all projects.

Agreed, I had to dig into a metaproj with vs code to understand it more, and even then it wasn't easy to parse.

benvillalobos avatar Jun 15 '22 18:06 benvillalobos

The -targets and -preprocess switches are both documented as not executing a build process. However, both will exclude a solution file from their respective operations and then incorrectly build the solution file.

It seems like the first pass of a fix would be to not build when either of these two switches are present. If the file is a solution, then the operation isn't performed because it isn't supported but also no build is performed. I have a sense already of how to make this fix.

Should -targets support solutions? I'm not sure that I fully understand the comments from @rainersigwald and @BenVillalobos but it seems the answer is 'Yes'.

Should -preprocess support solutions? Is -preprocess on a solution essentially the same output as setting MSBUILDEMITSOLUTION? Should a separate issue be created for the -preprocess switch with a solution?

jrdodds avatar Jul 30 '22 04:07 jrdodds

Should -targets support solutions?

Yes. In addition to "listing all the targets", I'd like to see a special case for solutions that also says "We'll make up a target for any target name you specify and pass it to all of the projects in the solution, so this isn't an exhaustive list".

Should -preprocess support solutions? Is -preprocess on a solution essentially the same output as setting MSBUILDEMITSOLUTION? Should a separate issue be created for the -preprocess switch with a solution?

IMO yes, yes, and yes. But this isn't super high pri IMO; I'd be quite happy with "it just errors out and doesn't accidentally build"

rainersigwald avatar Aug 01 '22 15:08 rainersigwald

@rainersigwald Thanks for the response. Please assign this issue to me.

I wasn't aware that it is now possible to pass a target to the solution that doesn't exist on the solution and that the solution will pass the target to all projects. I have had to work around this in the past. Now I understand the comment about the "dynamically-generated solution targets".

Thanks

jrdodds avatar Aug 01 '22 16:08 jrdodds

It was implemented in https://github.com/dotnet/msbuild/pull/1497, I believe.

rainersigwald avatar Aug 01 '22 16:08 rainersigwald

Some observations: Given a sol.sln that contains projects a.proj and b.proj;

The ability to pass project name and target to the solution (e.g the following code) is documented.

msbuild sol -target:b:fred

The ability to pass any target to the solution and the solution will invoke the target on each project (e.g the following code) does not appear to be in the documentation.

msbuild sol -target:fred

There is an issue. The build will have an error if any of the projects in the solution don't provide the target. i.e. if 'fred' is a target in b.proj but not in a.proj, then a MSB4057 error will be reported on a.proj. The solution has project specific targets but doesn't know to skip non-existent targets.

jrdodds avatar Aug 02 '22 22:08 jrdodds

@rainersigwald It may be helpful to split a new issue out from this issue:

  • [x] Don't build a solution when -preprocess or -targets is passed on the command line (addressed by #8588)

Implementing -targets for solution files would remain with this issue.

jrdodds avatar Aug 06 '22 20:08 jrdodds

Would this command also work with slnf ( solution filter files )?

Sorry for creating a duplicate issue, searching for "target" had way too many hits and I couldn't figure out how to search for what I wanted.

DerekSMorin avatar Sep 21 '22 15:09 DerekSMorin

We should make sure it works for .slnf too, yes. I suspect that might happen "for free" when fixing .sln but there might be some more work required.

rainersigwald avatar Sep 21 '22 15:09 rainersigwald

PR #8588 stops the incorrect behavior of building a solution when either -preprocess or -targets is provided.

Implementing -preprocess and -targets for solutions will be a separate PR on this issue.

jrdodds avatar Mar 22 '23 15:03 jrdodds

Was this closed too soon? The description of https://github.com/dotnet/msbuild/pull/8588 says it fixes this "Partially".

KalleOlaviNiemitalo avatar Apr 08 '23 05:04 KalleOlaviNiemitalo

Yes, this issue should be reopened.

The current PR 'fixes' the incorrect behavior of building the solution when a -preprocess or -targets switch is passed, but doesn't implement the switches for solutions.

jrdodds avatar Apr 08 '23 10:04 jrdodds

Whoops, didn't notice the keywords in the PR.

rainersigwald avatar Apr 10 '23 14:04 rainersigwald

(Noting that #8908 is related to this issue.)

jrdodds avatar Jun 26 '23 00:06 jrdodds