digital-asset / daml

The Daml smart contract language
https://www.digitalasset.com/developers
797 stars 199 forks source link

IDE breaks on modules with `{-# LANGUAGE CPP #-}` #17821

Open akrmn opened 10 months ago

akrmn commented 10 months ago

When opening a daml module that uses the CPP pragma, a red underline will appear at the bottom of the file, and the "Problems" tab will show the error "Main.daml: ❌ 22 in hpp-0.6.4:Hpp.CmdLine CPP". The full error is pasted below:

[{
    "resource": ".../buggy-cpp/daml/Main.daml",
    "owner": "_generated_diagnostic_collection_name_#2",
    "severity": 8,
    "message": "22 in hpp-0.6.4:Hpp.CmdLine\n",
    "source": "CPP",
    "startLineNumber": 105,
    "startColumn": 2147483648,
    "endLineNumber": 105,
    "endColumn": 2147483648
}]

Replicate

To replicate, run the commands below, then open Main.daml in vscode

daml new buggy-cpp

cat << EOF > buggy-cpp/daml/Main.daml
{-# LANGUAGE CPP #-}

module Main where
EOF

cd buggy-cpp; daml studio

Found while working on #17795

akrmn commented 10 months ago

Looking at the source code of hpp (https://github.com/acowley/hpp), one can see that the error on line 105 corresponds to an error call when the argument file does not exist. By overriding the cpp binary used by daml (with --cpp=fake_cpp) with a fake binary that simply logs the given arguments, I found that daml is calling hpp with an argument -iquote/<.../path/to/...>/buggy-cpp/daml/, but looking at the source code of hpp, there's no support for such arguments, so this is interpreted as the target file and thus hpp crashes because clearly there's no such file. Furthermore, using a version of hpp patched to recognize and ignore -iquote arguments, the error message didn't appear (though of course that's not a proper fix). This argument appears to be added by the following line in daml-ghcide,

https://github.com/digital-asset/daml-ghcide/blob/223e571d3cac214d131b85330bf09a1762e88671/src/Development/IDE/Core/Preprocessor.hs#L189