How to use regex-pcre2-1.0.0.0 with Stack?

Hello there,

I just recently started learning Haskell and its tooling.
Stack seems to be the way to go, and today is the first time I try to add a dependency to my project!
It is nothing else than “regex-prce2” because I want to use PRCE2 regex on my project
So I’ve added:


dependencies:
  - base >= 4.7 && < 5
  - regex-prce2 >= 1.0

to my package.yaml

But when I execute stack build I get the following error:


Error: [S-4804]
       Stack failed to construct a build plan.

       While constructing the build plan, Stack encountered
       the following errors. The 'Stack configuration' refers
       to the set of package versions specified by the
       snapshot (after any dropped packages, or pruned GHC
       boot packages; if a boot package is replaced, Stack
       prunes all other such packages that depend on it) and
       any extra-deps:

       In the dependencies for aoc2024-0.1.0.0:
         * regex-prce2 must match >=1.0, but no version is in
           the Stack configuration (no matching package and
           version found. Perhaps there is an error in the
           specification of a package's dependencies or
           build-tools (Hpack) or build-depends, build-tools
           or build-tool-depends (Cabal file) or an omission
           from the packages list in
           Documents/Projects/Personal/haskell/aoc2024/stack.yaml
           (project-level configuration).)
       The above is/are needed since aoc2024 is a build
       target.

This is very haunting, can someone give me a hint of whats going on? I assume that stack can’t find the package… but its published in Hackage, so I don’t get whats wrong

For more context, this is the stack snapshot I use:

snapshot: lts-22.43

2 Likes

I believe this is a typo in the package name: regex-pcre2

4 Likes

Dang, I feel so stupid, you are absolutely right… I’ve been in front of my screen for more than half an hour for this >,<

1 Like

No worries, it happens to me sometimes as well!

1 Like

Stack does not use Hackage directly. Instead it uses Stackage snapshots, which are lists of packages with fixed versions that are known to work well together. The regex-pcre2 package is unfortunately not in the snapshot that you are using. You can tell stack to use external packages by adding this in your stack.yaml file:

extra-deps:
- regex-pcre2-1.0.0.0

See the documentation here for more info:

2 Likes

True, I had to do that too,
Now the problem is that the dependency won’t even compile, there is some weird C code error:

regex-pcre2> error: clang: warning: -Wl,-no_fixup_chains: 'linker' input unused [-Wunused-command-line-argument]
regex-pcre2> clang: warning: -Wl,-no_warn_duplicate_libraries: 'linker' input unused [-Wunused-command-line-argument]
regex-pcre2> /private/var/folders/0y/jnhmlppn0yx0vknp95l_tn_00000gn/T/stack-186deed57de4e290/regex-pcre2-1.0.0.0/Wrap.hsc:369:84: error: use of undeclared identifier 'PCRE2_ALT_EXTENDED_CLASS'
regex-pcre2>   369 |     hsc_enum (CompOption, CompOption, hsc_printf ("%s", "compAltExtendedClass "),  PCRE2_ALT_EXTENDED_CLASS);
regex-pcre2>       |                                                                                    ^
regex-pcre2> /private/var/folders/0y/jnhmlppn0yx0vknp95l_tn_00000gn/T/stack-186deed57de4e290/regex-pcre2-1.0.0.0/Wrap.hsc:369:84: error: use of undeclared identifier 'PCRE2_ALT_EXTENDED_CLASS'
regex-pcre2> /private/var/folders/0y/jnhmlppn0yx0vknp95l_tn_00000gn/T/stack-186deed57de4e290/regex-pcre2-1.0.0.0/Wrap.hsc:369:84: error: use of undeclared identifier 'PCRE2_ALT_EXTENDED_CLASS'
regex-pcre2> 3 errors generated.

I guess I’ll try with another library then

Side question… can you recommend me a good Regex library just for matching simple regex?
I am just learning haskell by solving advent of code and I just need a simple regex for a simple match :slight_smile:

I’m sorry, I never use regexes in Haskell.

This might be a good learning opportunity. Haskell’s String are lists of characters, and so there’s a lot of matching logic that you can write as lists. For example, if you want to match strings where the first character is 'a', and the third character is either '1' or '5', you can write:

myMatch :: String -> Bool
myMatch ('a' : _ : somechar : _)
    | somechar == '1' || somechar == '5' = True
    | otherwise = False

Otherwise, looks like the regex-tdfa package is pure Haskell, which will be easier to install

3 Likes

That is great advice, thank you very much :slight_smile:

2 Likes

I also could not get regex-pcre2-1.0.0.0 (for PCRE2) to work.

In respect of the earlier version of PCRE, on Windows, I use this package.yaml for a toy single-package test project:

name:                regex-pcre-test
version:             0.1.0.0
dependencies:
- base >= 4.7 && < 5
- regex-pcre # Specify my package has this dependency ...

library:
  source-dirs: src

and this project-level configuration file (stack.yaml):

snapshot: lts-23.14 # GHC 9.8.4, and includes a regex-pcre version

stack build fails with a message about a missing C library:

regex-pcre     > Configuring regex-pcre-0.95.0.1...
regex-pcre     > Error: Cabal-simple_O_vy6YIf_3.10.3.0_ghc-9.8.4.exe: The pkg-config package
regex-pcre     > 'libpcre' is required but it could not be found.

Searching, I find that a MSYS2 package can supply that C library and so I add it to the Stack-supplied MSYS2 with:

stack exec -- pacman -S mingw-w64-x86_64-pcre

Now, stack build works as expected (extract only):

regex-pcre     > configure
regex-pcre     > Configuring regex-pcre-0.95.0.1...
regex-pcre     > build with ghc-9.8.4
...
regex-pcre     > Registering library for regex-pcre-0.95.0.1..
regex-pcre-test> configure (lib)
...
Registering library for regex-pcre-test-0.1.0.0..
Completed 2 action(s).

On Ubuntu (via WSL 2), a similar experience except obtaining the C library is done by:

sudo apt install libpcre3 libpcre3-dev
2 Likes

Note that there’s a difference between regex-pcre and regex-pcre2.

Understood. I’ve edited my contribution to make that clearer.

Alright so basically you have to install the missing C libraries into your system yourself
Not great but if it works it works :smiley:
I ended up using regex-tdfa, and although I was not familiar with POSIX-like regex style I ended up learning enough to solve my problem :slight_smile:
It was easier since regex-tdfa is fully built in Haskell so no C is required and it is already in the Stack snapshot apparently

1 Like

There are solutions for Haskell projects that involve a mix of dependencies on Haskell libraries and C libraries.

In particular, on Linux and macOS only, there is a package manager known as Nix. Stack can be configured to integrate with Nix, such that Nix handles the non-Haskell dependencies and Stack handles the Haskell dependencies (as usual). For futher information see: Nix integration - The Haskell Tool Stack and https://nixos.org/.

However, I don’t have much direct experience, as I am a Windows user. For my part, I mainly use the MSYS2 project to add C libraries to the Stack-supplied MSYS2.

If you want pcre2, there’s the pcre2 package (pcre2: Regular expressions via the PCRE2 C library (included)) which should work with pkg-config and the pcre2 lib installed. (I’m using it and it’s nice)

1 Like

I subsequently managed to get regex-pcre2-1.0.0.0 to compile on Windows. This is what I did:

First, Stack’s error message was:

regex-pcre2    > error: Wrap.hsc:369:84: error: use of undeclared identifier 'PCRE2_ALT_EXTENDED_CLASS'
regex-pcre2    >     hsc_enum (CompOption, CompOption, hsc_printf ("%s", "compAltExtendedClass "),  PCRE2_ALT_EXTENDED_CLASS);

I noted that the change log for the PCRE2 project says that PCRE2_ALT_EXTENDED_CLASS was introduced in version 10.45. I double-checked the MSYS2 version of the C library that I had: it was only 10.44.

I re-checked Base Package: pcre2 - MSYS2 Packages and it referred to version 10.45-1. So, I updated the Stack supplied MSYS2:

stack exec -- pacman -Syu

Then, the Haskell project compiled.

On Ubuntu 24.04.1 (Noble Numbat) (via WSL2) it was the same story: the version of PCRE2 obtained by apt install was not recent enough.

2 Likes