Skip to content

Fix CompilerError decoding in 0.13.x series of purs #635

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

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/App/API.purs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ compilePackage { packageSourceDir, compiler, resolutions } = Except.runExcept do
Log.debug "Compiling..."
compilerOutput <- Run.liftAff $ Purs.callCompiler
{ command: Purs.Compile { globs }
, version: Just (Version.print compiler)
, version: Just compiler
, cwd: Just packageSourceDir
}

Expand Down Expand Up @@ -834,7 +834,7 @@ publishToPursuit { packageSourceDir, dependenciesDir, compiler, resolutions } =
-- with the format used by Pursuit in PureScript versions at least up to 0.16
compilerOutput <- Run.liftAff $ Purs.callCompiler
{ command: Purs.Publish { resolutions: resolutionsFilePath }
, version: Just (Version.print compiler)
, version: Just compiler
, cwd: Just packageSourceDir
}

Expand Down
15 changes: 10 additions & 5 deletions app/src/App/CLI/Purs.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import Data.Codec.Argonaut.Record as CA.Record
import Data.Foldable (foldMap)
import Data.String as String
import Node.Library.Execa as Execa
import Registry.Version as Version

-- | Call a specific version of the PureScript compiler
callCompiler_ :: { version :: Maybe String, command :: PursCommand, cwd :: Maybe FilePath } -> Aff Unit
callCompiler_ :: { version :: Maybe Version, command :: PursCommand, cwd :: Maybe FilePath } -> Aff Unit
callCompiler_ = void <<< callCompiler

data CompilerFailure
Expand Down Expand Up @@ -83,7 +84,7 @@ printCompilerErrors errors = do
]

type CompilerArgs =
{ version :: Maybe String
{ version :: Maybe Version
, cwd :: Maybe FilePath
, command :: PursCommand
}
Expand Down Expand Up @@ -111,8 +112,7 @@ callCompiler compilerArgs = do
Just version ->
append "purs-"
$ String.replaceAll (String.Pattern ".") (String.Replacement "_")
$ fromMaybe version
$ String.stripPrefix (String.Pattern "v") version
$ Version.print version

errorsCodec = CA.Record.object "CompilerErrors"
{ errors: CA.array compilerErrorCodec }
Expand All @@ -122,7 +122,12 @@ callCompiler compilerArgs = do
Left { originalMessage }
| originalMessage == Just (String.joinWith " " [ "spawn", purs, "ENOENT" ]) -> Left MissingCompiler
Left { stdout, stderr } -> Left do
case parseJson errorsCodec stdout of
let
output = case compilerArgs.version of
Nothing -> stdout
Just version | Right min <- Version.parse "0.14.0", version < min -> stderr
Just _ -> stdout
case parseJson errorsCodec output of
Left err -> UnknownError $ String.joinWith "\n" [ stdout, stderr, CA.printJsonDecodeError err ]
Right ({ errors } :: { errors :: Array CompilerError })
| Array.null errors -> UnknownError "Non-normal exit code, but no errors reported."
Expand Down
3 changes: 1 addition & 2 deletions app/src/App/Effect/PackageSets.purs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ handle env = case _ of
compileInstalledPackages compiler = do
Log.debug "Compiling installed packages..."
let command = Purs.Compile { globs: [ Path.concat [ Path.basename packagesWorkDir, "**/*.purs" ] ] }
let version = Version.print compiler
Run.liftAff $ Purs.callCompiler { command, version: Just version, cwd: Just env.workdir }
Run.liftAff $ Purs.callCompiler { command, version: Just compiler, cwd: Just env.workdir }

attemptChanges :: Version -> PackageSet -> ChangeSet -> Run _ (Either CompilerFailure PackageSet)
attemptChanges compiler (PackageSet set) changes = do
Expand Down
21 changes: 12 additions & 9 deletions app/test/App/CLI/Purs.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ module Test.Registry.App.CLI.Purs (spec) where

import Registry.App.Prelude

import Data.Array as Array
import Data.Foldable (traverse_)
import Node.FS.Aff as FS.Aff
import Node.Path as Path
import Registry.App.CLI.Purs (CompilerFailure(..))
import Registry.App.CLI.Purs as Purs
import Registry.Foreign.Tmp as Tmp
import Registry.Test.Assert as Assert
import Registry.Test.Utils as Utils
import Registry.Version as Version
import Test.Spec as Spec

spec :: Spec.Spec Unit
spec = do
traverse_ testVersion [ "0.13.0", "0.14.0", "0.14.7", "0.15.4" ]
traverse_ testMissingVersion [ "0.13.1", "0.13.7", "0.15.1", "0.12.0", "0.14.12345" ]
testCompilationError
traverse_ (testVersion <<< Utils.unsafeVersion) [ "0.13.0", "0.14.0", "0.14.7", "0.15.4" ]
traverse_ (testMissingVersion <<< Utils.unsafeVersion) [ "0.13.1", "0.13.7", "0.15.1", "0.12.0", "0.14.12345" ]
traverse_ testCompilationError [ Just (Utils.unsafeVersion "0.13.0"), Just (Utils.unsafeVersion "0.13.8"), Just (Utils.unsafeVersion "0.14.0"), Just (Utils.unsafeVersion "0.15.0"), Nothing ]
where
testVersion version =
Spec.it ("Calls compiler version " <> version) do
Spec.it ("Calls compiler version " <> Version.print version) do
Purs.callCompiler { command: Purs.Version, cwd: Nothing, version: Just version } >>= case _ of
Left err -> case err of
MissingCompiler ->
Expand All @@ -28,21 +31,21 @@ spec = do
UnknownError err' ->
Assert.fail ("UnknownError: " <> err')
Right stdout ->
version `Assert.shouldEqual` stdout
Version.print version `Assert.shouldEqual` stdout

testMissingVersion version =
Spec.it ("Handles failure when compiler is missing " <> version) do
Spec.it ("Handles failure when compiler is missing " <> Version.print version) do
result <- Purs.callCompiler { command: Purs.Version, cwd: Nothing, version: Just version }
case result of
Left MissingCompiler -> pure unit
_ -> Assert.fail "Should have failed with MissingCompiler"

testCompilationError =
Spec.it "Handles compilation error for bad input file" do
testCompilationError version =
Spec.it ("Handles compilation error for bad input file for version " <> maybe "latest" Version.print version) do
tmp <- Tmp.mkTmpDir
let file = Path.concat [ tmp, "ShouldFailToCompile.purs" ]
FS.Aff.writeTextFile UTF8 file "<contents>"
result <- Purs.callCompiler { command: Purs.Compile { globs: [ file ] }, cwd: Nothing, version: Nothing }
result <- Purs.callCompiler { command: Purs.Compile { globs: [ file ] }, cwd: Nothing, version }
case result of
Left (CompilationError [ { position: { startLine: 1, startColumn: 1 } } ]) -> pure unit
_ -> Assert.fail "Should have failed with CompilationError"
2 changes: 1 addition & 1 deletion scripts/src/CompilerVersions.purs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ determineCompilerVersionsForPackage package version = do

result <- Run.liftAff $ Purs.callCompiler
{ command: Purs.Compile { globs: [ Path.concat [ formattedName, "src/**/*.purs" ] ] }
, version: Just (Version.print compiler)
, version: Just compiler
, cwd: Just tmp
}

Expand Down