cake-build / cake

:cake: Cake (C# Make) is a cross platform build automation system.
https://cakebuild.net
MIT License
3.89k stars 726 forks source link

C# 8 Using Statement produces compile error #2665

Closed richardnagle closed 2 years ago

richardnagle commented 4 years ago

What You Are Seeing?

When using the new C#8 using var... statement, the cake script produces compile errors.

What is Expected?

This script should compile successfully.

What version of Cake are you using?

0.35.0

Are you running on a 32 or 64 bit system?

64bit

What environment are you running on? Windows? Linux? Mac?

Windows 10

Are you running on a CI Server? If so, which one?

No

How Did You Get This To Happen? (Steps to Reproduce)

Task("C#8-Using-Statement")
    .Does(() =>
    {
        using var file = System.IO.File.Create(@".\temp.txt");
    });

Output Log

> .\build.ps1 -target C#8-Using-Statement

Preparing to run build script...
Running build script...
(5,11): error CS1002: ; expected
(5,7): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code
(5,11): error CS0103: The name 'file' does not exist in the current context
devlead commented 4 years ago

I believe this is likely an issue with the script pre-processing.

To unblock you for this specific use case as it's an stream that implements async disposable you can adjust your code to

Task("C#8-Using-Statement")
    .Does(async () =>
    {
        await using var file = System.IO.File.Create(@".\temp.txt");
    });
devlead commented 4 years ago

Just FYI Cake has a built in IO abstraction layer that handles OS/Environment specific things for you. i.e. path normalization. It will also mean you can take advantage of any module that enhances the filesystem i.e. long path, there's a fake file system in Cake.Testing etc.

Utilizing Cake FileSystem could i.e. look something like

Task("C#8-Using-Statement")
    .Does(async context =>
    {
        await using var file = context.FileSystem.GetFile("./temp.txt").OpenWrite();
    });
gitfool commented 2 years ago

I stumbled across this just now while trying the latest shiny C# with Cake version 2.0.0-rc0002: 😞

image

(Example from using statement (C# Reference))

devlead commented 2 years ago

I stumbled across this just now while trying the latest shiny C# with Cake version 2.0.0-rc0002: 😞

image

(Example from using statement (C# Reference))

Fix would be somewhere around here https://github.com/cake-build/cake/blob/0de2adc821dd65bc7c003d16c03878971c2698e6/src/Cake.Core/Scripting/Processors/UsingStatementProcessor.cs#L35

Some conditional to return false.

Maybe line contains ( and ) or something similar.

cake-build-bot commented 2 years ago

:tada: This issue has been resolved in version v2.0.0 :tada:

The release is available on:

Your GitReleaseManager bot :package::rocket: