Skip to content
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1394,12 +1394,12 @@ workspace:
# censor warnings if the code matches this code
# - { byPrefix } values:
# censor warnings if the warning's message
# starts with the given text
# starts with the given text.
- CodeName
# Note: when using `byPrefix`, use the `>` for block-string:
# see https://yaml-multiline.info/
- byPrefix: >
"Data.Map"'s `Semigroup instance`
Data.Map's `Semigroup` instance

# Specify whether to show statistics at the end of the compilation,
# and how verbose they should be.
Expand Down Expand Up @@ -1450,12 +1450,12 @@ package:
# censor warnings if the code matches this code
# - { byPrefix } values:
# censor warnings if the warning's message
# starts with the given text
# starts with the given text.
- CodeName
# Note: when using `byPrefix`, use the `>` for block-string:
# see https://yaml-multiline.info/
- byPrefix: >
"Data.Map"'s `Semigroup instance`
Data.Map's `Semigroup` instance
# Convert compiler warnings for files in this package's src code
# into errors that can fail the build.
# Optional and defaults to false
Expand Down Expand Up @@ -1518,12 +1518,12 @@ package:
# censor warnings if the code matches this code
# - { byPrefix } values:
# censor warnings if the warning's message
# starts with the given text
# starts with the given text.
- CodeName
# Note: when using `byPrefix`, use the `>` for block-string:
# see https://yaml-multiline.info/
- byPrefix: >
"Data.Map"'s `Semigroup instance`
Data.Map's `Semigroup` instance
# Convert compiler warnings for files from this package's test code
# into errors that can fail the build.
# Optional and defaults to false
Expand Down
23 changes: 22 additions & 1 deletion src/Spago/Psa.purs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,29 @@ shouldPrintWarning = case _ of
let
tests = arr <#> case _ of
ByCode c -> \code _ -> c == code
ByMessagePrefix prefix -> \_ msg -> isJust $ String.stripPrefix (String.Pattern $ String.trim prefix) (String.trim msg)
ByMessagePrefix prefix -> \code msg ->
let
trimmedPrefix = String.trim prefix
trimmedMsg = String.trim msg
-- Try direct match first
directMatch = isJust $ String.stripPrefix (String.Pattern trimmedPrefix) trimmedMsg
-- For UserDefinedWarning, also try matching without the compiler preamble
strippedMatch = code == "UserDefinedWarning"
&& isJust (String.stripPrefix (String.Pattern trimmedPrefix) (stripUserWarningPreamble trimmedMsg))
in
directMatch || strippedMatch
-- We return `true` to print the warning.
-- If an element was found (i.e. `Just` is returned), then one of the tests succeeded,
-- so we should not print the warning and return false here.
\code msg -> isNothing $ NonEmptyArray.find (\f -> f code msg) tests

-- | Strip the preamble that the PureScript compiler adds to UserDefinedWarning messages.
-- | This allows byPrefix to match just the user-defined content.
stripUserWarningPreamble :: String -> String
stripUserWarningPreamble msg =
let
preamble = "A custom warning occurred while solving type class constraints:"
in
case String.stripPrefix (String.Pattern preamble) msg of
Just rest -> String.trim rest
Nothing -> msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package:
name: censor-user-warning-test
dependencies:
- prelude

workspace:
packageSet:
registry: 41.5.0
11 changes: 11 additions & 0 deletions test-fixtures/build/censor-user-defined-warning/spago.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package:
name: censor-user-warning-test
dependencies:
- prelude
build:
censorProjectWarnings:
- byPrefix: "This is a custom warning"

workspace:
packageSet:
registry: 41.5.0
17 changes: 17 additions & 0 deletions test-fixtures/build/censor-user-defined-warning/src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Main where

import Prelude
import Prim.TypeError (class Warn, Text)

-- A type class with a Warn constraint that triggers a UserDefinedWarning
class MyClass a where
myFunction :: a -> a

instance warnedInstance ::
( Warn (Text "This is a custom warning that should be censored")
) => MyClass Int where
myFunction = identity

-- Using the instance triggers the warning at compile time
main :: Int
main = myFunction 42
30 changes: 30 additions & 0 deletions test/Spago/Build.purs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ spec = Spec.around withTempDir do
, result: isRight
}

Spec.it "should censor UserDefinedWarning with byPrefix matching just the user content" \{ spago, fixture, testCwd } -> do
FS.copyTree { src: fixture "build/censor-user-defined-warning", dst: testCwd </> "." }

let
warningTexts =
[ escapePathInErrMsg [ "src", "Main.purs" ]
, "UserDefinedWarning"
, "A custom warning occurred while solving type class constraints"
, "This is a custom warning that should be censored"
]
shouldHaveWarning = assertWarning warningTexts true
shouldNotHaveWarning = assertWarning warningTexts false

-- First, verify the warning IS censored with byPrefix matching user content
spago [ "build" ] >>= check
{ stdout: mempty
, stderr: shouldNotHaveWarning
, result: isRight
}

-- Now remove the censor config and verify the warning DOES appear
FS.unlink $ testCwd </> "spago.yaml"
FS.moveSync { src: testCwd </> "spago-no-censor.yaml", dst: testCwd </> "spago.yaml" }
rmRf $ testCwd </> "output"
spago [ "build" ] >>= check
{ stdout: mempty
, stderr: shouldHaveWarning
, result: isRight
}

Spec.describe "lockfile" do
Spec.it "building with a lockfile doesn't need the Registry repo" \{ spago, fixture, testCwd } -> do
spago [ "init", "--name", "aaa", "--package-set", "33.0.0" ] >>= shouldBeSuccess
Expand Down
Loading