Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

unable to use mapster.sourcegen in .net9

Open rendeecpbuild opened this issue 7 months ago • 2 comments

rendeecpbuild avatar May 21 '25 16:05 rendeecpbuild

Same here. During package install I get these errors:

NU1202: Package Mapster.Tool 8.4.2-pre02 is not compatible with net9.0 (.NETCoreApp,Version=v9.0). Package Mapster.Tool 8.4.2-pre02 supports:
  - net8.0 (.NETCoreApp,Version=v8.0) / any
  - net9.0 (.NETCoreApp,Version=v9.0) / any
NU1212: Invalid project-package combination for Mapster.Tool 8.4.2-pre02. DotnetToolReference project style can only contain references of the DotnetTool type 
Package 'Mapster.Tool 8.4.2-pre02' has a package type 'DotnetTool' that is not supported by project 

My project has a target framework of net9.

Soundman32 avatar May 22 '25 08:05 Soundman32

Are you trying to add Mapster.Tool as a reference in your project files?

andrerav avatar May 22 '25 09:05 andrerav

trying this today and i can't generate dtos

project structure

C:\path\to\MapsterToolPlayground
│   MapsterToolPlayground.sln
├───.config
│       dotnet-tools.json
└───MapsterToolPlayground
    │   MapsterToolPlayground.csproj
    │   Program.cs
    ├───Domain
    │       Person.cs

dotnet-tools.json

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "mapster.tool": {
      "version": "8.4.0",
      "commands": [
        "dotnet-mapster"
      ],
      "rollForward": false
    }
  }
}

*.csproj

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net9.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Mapster" Version="7.4.0" />
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	</ItemGroup>

	<ItemGroup>
		<Generated Include="**\*.g.cs" />
	</ItemGroup>
	<Target Name="CleanGenerated">
		<Delete Files="@(Generated)" />
	</Target>
	<Target Name="Mapster">
		<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet build" />
		<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
		<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster model -a $(TargetDir)$(ProjectName).dll -n MapsterToolPlayground.Dtos -o Dtos -r" />
		<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a $(TargetDir)$(ProjectName).dll -n MapsterToolPlayground.Dtos -o Dtos" />
		<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a $(TargetDir)$(ProjectName).dll -n MapsterToolPlayground.Mappers -o Mappers" />
	</Target>

</Project>

domain class

using Mapster;
using Newtonsoft.Json;

namespace MapsterToolPlayground.Domain
{
    [AdaptTwoWays("[name]Dto"), GenerateMapper]
    public class Person
    {
        [JsonConstructor]
        public Person()
        {

        }

        public Person(string id, string name, int age, DateTime createdOn = default)
        {
            Id = id;
            Name = name;
            Age = age;
            CreatedOn = createdOn == default ? DateTime.UtcNow : createdOn;
        }

        public string Id { get; protected set; }
        public string Name { get; protected set; }

        public Person SetName(string name)
        {
            Name = name;
            return this;
        }

        public int Age { get; protected set; }

        public Person SetAge(int age)
        {
            Age = age;
            return this;
        }

        public DateTime CreatedOn { get; protected set; }
        public Person SetCreatedOn(DateTime createdOn = default)
        {
            CreatedOn = createdOn == default ? DateTime.UtcNow : createdOn;
            return this;
        }
        public DateTime? UpdatedOn { get; protected set; }
        public Person SetUpdatedOn(DateTime? updatedOn = null)
        {
            UpdatedOn = updatedOn == null ? DateTime.UtcNow : updatedOn;
            return this;
        }
    }
}

Program.cs

namespace MapsterToolPlayground
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

running dotnet msbuild -t:Mapster nothing happens, no output running dotnet mapster model -a .\MapsterToolPlayground\bin\Debug\net9.0\MapsterToolPlayground.dll nothing happens, no output

triying to add Mapster.Tool nuget i get

NU1202: Package Mapster.Tool 8.4.0 is not compatible with net9.0 (.NETCoreApp,Version=v9.0). Package Mapster.Tool 8.4.0 supports:
  - net6.0 (.NETCoreApp,Version=v6.0) / any
  - net7.0 (.NETCoreApp,Version=v7.0) / any
NU1212: Invalid project-package combination for Mapster.Tool 8.4.0. DotnetToolReference project style can only contain references of the DotnetTool type 
Package 'Mapster.Tool 8.4.0' has a package type 'DotnetTool' that is not supported by project 'MapsterToolPlayground'.

What i am doing wrong?

thanks in advance

jjavierdguezas avatar Aug 04 '25 16:08 jjavierdguezas

changing in csproj

-		<TargetFramework>net9.0</TargetFramework>
+		<TargetFramework>net7.0</TargetFramework>

and it works, so is this package not compatible with net9.0 ?

jjavierdguezas avatar Aug 04 '25 16:08 jjavierdguezas

Are you using Mapster 7.5.0 and Mapster.Tool 8.5.0?

stagep avatar Aug 04 '25 17:08 stagep

Are you using Mapster 7.5.0 and Mapster.Tool 8.5.0?

@stagep

just what you see in my comment

<PackageReference Include="Mapster" Version="7.4.0" />	

and

   "mapster.tool": {
      "version": "8.4.0",

following the steps in the wiki https://github.com/MapsterMapper/Mapster/wiki/Mapster.Tool

jjavierdguezas avatar Aug 04 '25 17:08 jjavierdguezas

Are you using Mapster 7.5.0 and Mapster.Tool 8.5.0?

@stagep

trying to force Mapster 7.5.0 and i get this error

Image

the latest version on https://github.com/MapsterMapper/Mapster/releases are Mapster 7.4.0 and Mapster.Tool 8.4.0

jjavierdguezas avatar Aug 04 '25 17:08 jjavierdguezas

Did you put the Target Framework back to 9.0?

Image

stagep avatar Aug 04 '25 17:08 stagep

Did you put the Target Framework back to 9.0?

Image

yes I did, but I'll try it again later and I'll report back

jjavierdguezas avatar Aug 04 '25 18:08 jjavierdguezas

Did you put the Target Framework back to 9.0?

Image

@stagep i don't know where do you get that version, but I cant. could you provide the nuget url? that version isnt listed there 🤷🏻‍♂️

https://www.nuget.org/packages/Mapster#versions-body-tab

jjavierdguezas avatar Aug 04 '25 19:08 jjavierdguezas

My bad. 6 months ago, I had taken a version of Mapster 7.4 / Mapster.Tool 8.4 and loaded on my machine and upgraded these to .Net 9. One of my nuget feeds is to the output of that solution. I had done this because I was having an issue using Mapster.Tools on .Net 9. I would try the latest prerelease versions of both Mapster and Mapster.Tools to see if it works for you on .Net 9. Please let me know either way. Thanks.

stagep avatar Aug 04 '25 19:08 stagep

My bad. 6 months ago, I had taken a version of Mapster 7.4 / Mapster.Tool 8.4 and loaded on my machine and upgraded these to .Net 9. One of my nuget feeds is to the output of that solution. I had done this because I was having an issue using Mapster.Tools on .Net 9. I would try the latest prerelease versions of both Mapster and Mapster.Tools to see if it works for you on .Net 9. Please let me know either way. Thanks.

thanks @stagep for your help

making this changes:

csproj

- 		<TargetFramework>net7.0</TargetFramework>
+		<TargetFramework>net9.0</TargetFramework>

...

-		<PackageReference Include="Mapster" Version="7.4.0" />
+		<PackageReference Include="Mapster" Version="7.4.2-pre02" />

dotnet-tools.json

-       "version": "8.4.0",
+       "version": "8.4.2-pre02",

i can cofirm that the latest pre release version of Mapster and Mapster.Tools work just fine with net9.0

thxs!

jjavierdguezas avatar Aug 04 '25 19:08 jjavierdguezas

Thank you. I had forgotten I was using my own fork so now I will move to the prerelease version and delete my local version.

stagep avatar Aug 04 '25 19:08 stagep