janet-lang / jpm

Janet Project Manager
MIT License
70 stars 22 forks source link

JPM breaks its launcher script during bootstrap #92

Open na-sa-do opened 2 months ago

na-sa-do commented 2 months ago

/usr/local/bin/jpm begins with this:

#!/usr/local/bin/janet
#!/usr/bin/env janet

which is obviously nonsense, and prevents JPM from running, since my Janet is at /usr/bin/janet. Deleting the first shebang line fixes the issue.

sogaiu commented 2 months ago

My jpm looks a bit odd at the beginning as well:

#!/home/user/.local/bin/janet
#!/usr/bin/env janet
(put root-env :syspath "/home/user/.local/lib/janet")
(import jpm/cli)
# Reset the syspath after overriding
(put root-env :syspath (os/getenv "JANET_PATH" (dyn :syspath)))
(defn main [& argv]
  (cli/main ;argv))

How did you install jpm?

FWIW, I typically do:

PREFIX=$HOME/.local janet bootstrap.janet

or use the install-jpm-git ``Makefile target in the janet repository.

na-sa-do commented 2 months ago

I just ran doas janet bootstrap.janet. I didn't set PREFIX or anything.

sogaiu commented 2 months ago

Thanks for the details.

It's possible it might have something to do with: https://github.com/janet-lang/jpm/blob/d93b7c243645d31410a81fb9ab8f7a5e5608f0d0/configs/bsd_config.janet#L1

Looks like the default for prefix is /usr/local.

Though that's from a config that looks like it's for BSDs, the linux and macos ones also have the same value for prefix.

Are you using some kind of BSD system?

na-sa-do commented 2 months ago

I'm on Chimera Linux, which does have a somewhat BSD-ian setup, but it's not BSD.

sogaiu commented 2 months ago

Ah I see. That's not one I'm familiar with, but I gave one of the live isos a try.

At least using PREFIX=$HOME/.local and the aforementioned steps above seems to work ok here. Though the beginning of jpm does look weird with two shebang lines.

I also tried without PREFIX=... and it worked there too.

For the latter case, my jpm looks like:

#!/usr/local/bin/janet
#!/usr/bin/env janet
(put root-env :syspath "/usr/local/lib/janet")
(import jpm/cli)
# Reset the syspath after overriding
(put root-env :syspath (os/getenv "JANET_PATH" (dyn :syspath)))
(defn main [& argv]
  (cli/main ;argv))

Possibly obvious but janet ended up under /usr/local/bin -- I wonder how yours did not. Curious.


Update: forgot to mention I built janet from source after cloning its repository.

na-sa-do commented 2 months ago

Chimera packages Janet, so naturally they put it directly under /usr. That's the point of the split, AFAIK.

sogaiu commented 2 months ago

Just a guess, but I think then it might be that things might work better if PREFIX=/usr was used when trying to install jpm.

Though, may be that's not really that great from the perspective of working with the packaging system.

Or may be there is a package for jpm? Didn't find one via this...


Update:

Options that seem like they might work better include:

  1. Someone makes an appropriate package for jpm
  2. Use a non-system install of janet and a corresponding install of jpm.

May be others will have additional / better ideas...


Update 2: To give additional info about why just changing the first line of jpm might not work out so well:

The bootstrapping process can also be configured by setting PREFIX to install to a different system directory. Generally, you will want to install to the same directory that Janet was installed to so jpm can find the required headers and libraries for compiling C libraries.

via the Self Installation (Bootstrapping) section of the README.

bakpakin commented 2 months ago

Yes, the issue here is the JPM config thinks that the PREFIX of janet is set to /usr/local.

What is the output of jpm show-paths?

na-sa-do commented 2 months ago

The original installation gives:

tree:       
binpath:    /usr/local/bin
modpath:    /usr/lib/janet
syspath:    /usr/lib/janet
manpath:    /usr/local/share/man/man1
libpath:    /usr/local/lib
headerpath: /usr/local/include/janet
buildpath:  build/
gitpath:    git
tarpath:    tar
curlpath:   curl

Reinstalling with PREFIX=/usr gives:

tree:       
binpath:    /usr/bin
modpath:    /usr/lib/janet
syspath:    /usr/lib/janet
manpath:    /usr/share/man/man1
libpath:    /usr/lib
headerpath: /usr/include/janet
buildpath:  build/
gitpath:    git
tarpath:    tar
curlpath:   curl

(and doesn't require manually editing the jpm launcher.)

There is no package for JPM, AFAIK. I could probably make one -- I've made a few for Chimera already -- but I've barely tried Janet in the first place, so I'm not sure I want to sign up for that yet. Besides which, it would probably be best to actually fix the bug here instead of in Chimera, since the bug's likely to reappear in any distro that packages Janet but forgets JPM.

Also, I don't know enough about JPM's internals to be sure, but judging from these paths I think both JPMs want to install global libraries to /usr instead of /usr/local? That seems like a bad idea, and would definitely need fixing in order to package JPM.

pyrmont commented 1 month ago

I think the cause is here:

https://github.com/janet-lang/jpm/blob/d93b7c243645d31410a81fb9ab8f7a5e5608f0d0/jpm/make-config.janet#L20-L29

When JPM's bootstrap generates a config file, it tries to guess the prefix by (1) checking the JANET_PREFIX environment variable, (2) checking the PREFIX environment variable and (3) attempting to guess the path to janet at the executable used to run bootstrap.janet.

The problem is that if you start Janet by typing janet (as @na-sa-do said he did and as JPM's README instructs you to do), then (dyn :executable) will return simply janet. This will fall through to the default branch in the cond in prefix-guess and cause JPM to use /usr/local/ as the prefix.

The solution to this is presumably to do some kind of search through the PATH of the user to work out the absolute path to the executable if it's invoked with a non-absolute path.

alternateved commented 4 weeks ago

Besides which, it would probably be best to actually fix the bug here instead of in Chimera, since the bug's likely to reappear in any distro that packages Janet but forgets JPM.

I can confirm that the same issue happens on Fedora.

sogaiu commented 4 weeks ago

@alternateved Is there a janet package for Fedora?

FWIW, if you are building from source (or are willing to), the setup I use is very close to what is described here. I typically use PREFIX=$HOME/.local. May be that's an option?

alternateved commented 4 weeks ago

@sogaiu Yes, there is: https://src.fedoraproject.org/rpms/janet

When using Fedora's janet package jpm cannot be installed with PREFIX=$HOME/.local. Only PREFIX=/usr somehow works properly, but I cannot confirm this fully, as I wasn't even able to install spork with it (most likely janet is improperly packaged on Fedora janet.h couldn't be located during install and it was failing).

When using PREFIX=$HOME/.local I get this:

~/Code/jpm $ PREFIX=$HOME/.local janet bootstrap.janet
destdir: 
Using install prefix: /home/alternateved/.local
binpath: /home/alternateved/.local/bin
libpath: /home/alternateved/.local/lib
manpath: /home/alternateved/.local/share/man/man1
headerpath: /home/alternateved/.local/include/janet
modpath: (default to JANET_PATH at runtime)
Setting package listing: https://github.com/janet-lang/pkgs.git
Running jpm to self install...
removing /usr/lib64/janet/jpm/cc.janet
error: Permission denied: /usr/lib64/janet/jpm/cc.janet
error: build fail
  in pdag [jpm/dagbuild.janet] (tailcall) on line 79, column 23
  in run [jpm/cli.janet] (tailcall) on line 88, column 9
  in run-main [boot.janet] on line 3936, column 16
  in cli-main [boot.janet] on line 4089, column 17
error: command failed with non-zero exit code 1
  in os/execute [src/core/os.c] on line 1360
  in do-bootstrap [bootstrap.janet] on line 14, column 3
  in _thunk [bootstrap.janet] (tailcall) on line 26, column 1
alternateved commented 4 weeks ago

Ah, I just realised that in order to have janet.h I also need to install janet-devel package. So failure to install spork is my mistake. But the rest is still an issue.

sogaiu commented 4 weeks ago

@alternateved Yes:

When using Fedora's janet package jpm cannot be installed with PREFIX=$HOME/.local

is expected AFAIU. My understanding is based on jpm's README:

Generally, you will want to install to the same directory that Janet was installed to so jpm can find the required headers and libraries for compiling C libraries.

I presume Fedora's package essentially uses a PREFIX of /usr. IMO, it's problematic to try to use a non-packaged jpm with this kind of setup because in my experience, roughly, OS packaging systems aren't keen on having things directly installed under /usr, /usr/bin, etc. (apart from /usr/local).

So I think there is a kind of conflict here.

As I mentioned in this comment, I think there are at least two ways that might resolve the situation:

  1. Someone packages jpm appropriately for the distribution in question, or
  2. Use a common PREFIX that points outside of /usr, (except if using /usr/local -- but that's the default IIUC), say $HOME/.local.

The intention of my suggestion was to use the second approach, i.e. a source build for both Janet and JPM, so they would end up using the same PREFIX. FWIW, the linked instructions here, cover both Janet and JPM.