Skip to content
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

Questions about prefix-independence for data files #7888

Open
danidiaz opened this issue Dec 31, 2021 · 5 comments
Open

Questions about prefix-independence for data files #7888

danidiaz opened this issue Dec 31, 2021 · 5 comments
Labels

Comments

@danidiaz
Copy link
Collaborator

I've been following the documentation for the Paths_pkgname and data-files mechanism and managed to get a simple example going.

However, I don't quite understand the documentation for prefix-independence:

On Windows it is possible to obtain the pathname of the running program.

Does that mean that prefix-independence is only for Windows? Perhaps it should be explicitly stated.

The executable can find its auxiliary files by finding its own path and knowing the location of the other files relative to $bindir. Prefix-independence is particularly useful: it means the user can choose the install location (i.e. the value of $prefix) at install-time

Here by "installing", do we mean something like copying the executable to another location, maybe across machines?

If the executable finds the other files relative to $bindir, why is $prefix needed at all?

In order to achieve this, we require that for an executable on Windows, all of $bindir, $libdir, $dynlibdir, $datadir and $libexecdir begin with $prefix.

Is this the default, or do I have to do tweak something in order to achieve it? Does "beginning with $prefix" mean "beginning with the literal text '$prefix'" or "beginning with the value of $prefix"?

System information

  • Cabal 3.6.2.0
@Mikolaj
Copy link
Member

Mikolaj commented Jan 3, 2022

This may be a problem not only with documentation. I've heard from @phadej the data files feature is not fully working or not for all OSes and there is no obvious way forward. I guess studying open issues about that might indicate why. Indeed, if would be great to at least document what is failing.

@danidiaz
Copy link
Collaborator Author

danidiaz commented Jan 3, 2022

Working on Windows, and looking at my generated Paths_ module, I see code like:

getDataFileName :: FilePath -> IO FilePath
getDataFileName name = do
  dir <- getDataDir
  return (dir `joinFileName` name)

getDataDir    = catchIO (getEnv "dataexp_datadir")    (\_ -> return datadir)

datadir    = "C:\\Users\\MyUser\\.local\\cabal\\x86_64-windows-ghc-8.10.7\\dataexp-0.1.0.0"

So it seems that the data dir is hardwired, but it can be overridden using an environment variable.

I don't see anything related to finding the data dir by asking the OS for the path of the executable (as implied by the prefix-independence docs) but perhaps it would exist if I had used some non-default compilation option.

@danidiaz
Copy link
Collaborator Author

danidiaz commented Jan 3, 2022

In the Cabal source code, the file templates\Paths_pkg.template.hs does have code like the following:

{% if relocatable %}

getPrefixDirReloc :: FilePath -> IO FilePath
getPrefixDirReloc dirRel = do
  exePath <- getExecutablePath
  let (dir,_) = splitFileName exePath
  return ((dir `minusFileName` {{ bindir }}) `joinFileName` dirRel)

But how to enable relocatable?

Apparently, there's a relocatable field in cabal.project, since Cabal 1.22. It's set to False by default. I tried setting it to True but it doesn't seem to do anything; getPrefixDirReloc still doesn't appear in my Paths_ file.

@Mikolaj
Copy link
Member

Mikolaj commented Jan 3, 2022

Uhoh, so the feature is not only undocumented and not quite working, but there are also bits and pieces of its carcass laying around and tripping contributors? That definitely requires an intervention.

@Mikolaj Mikolaj added attention: needs-help Help wanted with this issue/PR Cabal: other re: project-file Concerning cabal.project files labels Jan 3, 2022
@jneira
Copy link
Member

jneira commented Jun 11, 2022

it seems to be a bug, as the hardwired path is wrong
"C:\\Users\\MyUser\\.local\\cabal\\x86_64-windows-ghc-8.10.7\\dataexp-0.1.0.0" should be "C:\\Users\\MyUser\\.local\\cabal\\packages\\xxxx\\x86_64-windows-ghc-8.10.7\\dataexp-0.1.0.0" or something alike (will check the exact path)

I did some chackes with:

module Main where
import Paths_datadirs


main :: IO ()
main = do
  dir <- getDataDir
  putStrLn $ "Datadir: " ++ dir
PS D:\dev\ws\haskell\issues\datadirs> cabal run
Datadir: D:\dev\ws\haskell\issues\datadirs\.\.
PS D:\dev\ws\haskell\issues\datadirs> cabal install
....
Copying 'datadirs.exe' to 'D:\bin\datadirs.exe'
PS D:\dev\ws\haskell\issues\datadirs> datadirs
Datadir: D:\cabal\store\ghc-8.10.7\datadirs-0.1.0.0-8741e1327c97f40d292496f63ccc5862cd3e0d46\share

Of course you need data-files filed filled to make cabal copy the files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants