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

Unable to set Antlr4 Build Action in .Net Core 2 project in Visual Studio 2017 #250

Closed
jaydeept opened this issue Oct 3, 2017 · 8 comments
Labels

Comments

@jaydeept
Copy link

jaydeept commented Oct 3, 2017

I have created a .Net Core 2 project and wanted to add Grammar.g4 file. I have added Nuget packages called "ANtlr4", "Antlr4.Runtime" and "Antlr4.CodeGenerator" with latest available version 4.6.4. Somehow, I am not able to change "Build Action" to Antlr4 for my Grammar.g4 file. Because of that, it's not generating parser and lexer files. Can anyone please help here what's wrong going on?

I tried to modify csproj file manually but still it didn't generate lexer and parser file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <AssemblyName>Pds.QueryLanguageGrammar</AssemblyName>
    <RootNamespace>Pds.QueryLanguageGrammar</RootNamespace>
    <DocumentationFile>..\..\build\doc\xml\Pds.QueryLanguageGrammar.xml</DocumentationFile>
    <OutputPath>..\..\build\$(Configuration)\Pds\</OutputPath>
    <BaseIntermediateOutputPath>..\..\work\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Antlr4 Include="Grammar.g4">
      <Generator>MSBuild:Compile</Generator>
      <CustomToolNamespace>Pds.QueryLanguageGrammar</CustomToolNamespace>
      <Listener>False</Listener>
      <Visitor>True</Visitor>
    </Antlr4>
  </ItemGroup>
</Project>
@sharwell
Copy link
Member

sharwell commented Oct 3, 2017

It looks like your project does not contain any reference to ANTLR 4. You'll need to reference two packages in order to compile a project with a grammar:

  1. Antlr4.CodeGenerator: This package generates code for a grammar
  2. Antlr4.Runtime: This package contains the runtime library so the generated grammar code will compile

📝 The Antlr4 package is a metadata package that allows you to reference both of the above using a single reference.

Adding the following to your project file should resolve the issue:

<PropertyGroup>
  <!-- See the release notes for 4.6.5-beta001 for an explanation of this optional property -->
  <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>
</PropertyGroup>

<ItemGroup>
  <!-- This is the required reference which is missing from your project file -->
  <PackageReference Include="Antlr4" Version="4.6.5-beta001" />
</ItemGroup>

@jaydeept
Copy link
Author

jaydeept commented Oct 3, 2017

I have modified my csproj like this. Still it hasn't generated required files.
I am seeing that Antlr 4.6.4 is latest available. How can I get 4.6.5-beta001?

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <AssemblyName>Pds.QueryLanguageGrammar</AssemblyName>
    <RootNamespace>Pds.QueryLanguageGrammar</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <Antlr4 Include="Grammar.g4">
      <Generator>MSBuild:Compile</Generator>
      <CustomToolNamespace>Pds.QueryLanguageGrammar</CustomToolNamespace>
      <Listener>True</Listener>
      <Visitor>False</Visitor>
    </Antlr4>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Antlr4" Version="4.6.4" />
    <PackageReference Include="Antlr4.CodeGenerator" Version="4.6.4" />
    <PackageReference Include="Antlr4.Runtime" Version="4.6.4" />
  </ItemGroup>
  <PropertyGroup>
      <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>
  </PropertyGroup>
</Project>

@sharwell
Copy link
Member

sharwell commented Oct 3, 2017

@jaydeept You need to replace your current PackageReference lines with the one mentioned in my comment above. The 4.6.4 release is missing commit bcad85a from #233, which is required for working with the new project system.

@jaydeept
Copy link
Author

jaydeept commented Oct 3, 2017

Hi @sharwell ,

Gotcha, I installed NuGet package using below command:
Install-Package Antlr4 -Version 4.6.5-beta001
and was able to get rid of warning it was showing earlier in Visual Studio, when I modified csproj file manually. Here is my new csproj file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <AssemblyName>Pds.QueryLanguageGrammar</AssemblyName>
    <RootNamespace>Pds.QueryLanguageGrammar</RootNamespace>
	<Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Antlr4" Version="4.6.5-beta001" />
  </ItemGroup>
  <ItemGroup>
    <Antlr4 Include="Grammar.g4">
      <Generator>MSBuild:Compile</Generator>
      <CustomToolNamespace>Pds.QueryLanguageGrammar</CustomToolNamespace>
      <Listener>True</Listener>
      <Visitor>False</Visitor>
    </Antlr4>
  </ItemGroup>
</Project>

However, I am still not seeing the parser and lexer file. Do I still need to add anything in csproj file?

@sharwell
Copy link
Member

sharwell commented Oct 3, 2017

However, I am still not seeing the parser and lexer file

They are part of the build, but not visible in Solution Explorer. If you create a new class you'll be able to use them just fine. 😄

@jaydeept
Copy link
Author

jaydeept commented Oct 4, 2017

@sharwell ,
I finally found the issue. I did modify

BaseIntermediateOutputPath

in csproj file and hence it was not generating required file 🤦‍♂️
I needed to apply some workaround to resolve the issue.

@sharwell
Copy link
Member

sharwell commented Oct 4, 2017

I did modify BaseIntermediateOutputPath in csproj file and hence it was not generating required file

Modifying this property should not cause any problems for the code generator. If it did, then it sounds like you found a bug.

I needed to apply some workaround to resolve the issue.

Can you be more specific?

@jaydeept
Copy link
Author

jaydeept commented Oct 4, 2017

Hi @sharwell,
This is the same issue like one mentioned here.
I did face that issue for NSubstitute as well, but applying that workaround has resolved this issue too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants