kscripting / kscript

Scripting enhancements for Kotlin
MIT License
2.08k stars 126 forks source link

Bootstrap headers not working (bare centeos) #324

Closed robert2411 closed 2 years ago

robert2411 commented 3 years ago

When I run a simple script with the bootstrap headers I get the folloing error: ./test.kts: line 6: exec: kscript: not found

I created a reproducable in docker

FROM centos
RUN yum install unzip -y
RUN yum install zip -y
WORKDIR /project
COPY ./test.kts /project
RUN chmod +x test.kts
RUN ./test.kts

the script

#!/bin/bash

//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || curl -L "https://git.io/fpF1K" | bash 1>&2
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/

println("Hello from Kotlin!")
for (arg in args) {
    println("arg: $arg")
}
holgerbrandl commented 3 years ago

Hmm, I can't reproduce the problem yet. I've uninstalled kscript and ran your script, which works fine: It installs kscript via sdkman and runs the script afterwards.

The install procedure is pulled from https://raw.githubusercontent.com/holgerbrandl/kscript/master/misc/boostrap_header.sh so maybe this fails in your environment?

wuseal commented 3 years ago

Same problem here

Looking for a previous installation of SDKMAN...
Looking for unzip...
Looking for zip...
Not found.
======================================================================================================
 Please install zip on your system using your favourite package manager.

 Restart after installing zip.
======================================================================================================

$ sdk install java

System info

LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.7.1908 (Core)
Release:    7.7.1908
Codename:   Core

install failed

After install zip manually , the same problem throw out as @robert2411 post

Also not only kscript, java, kotlin and gradle also can't found

And it's the same problem with run : https://raw.githubusercontent.com/holgerbrandl/kscript/master/misc/boostrap_header.sh

When manually run source /root/.sdkman/bin/sdkman-init.sh after runing the script, It works

holgerbrandl commented 3 years ago

Could it be that $HOME is not defined? This may cause such kind of confusion, see https://git.io/fpF1K

wuseal commented 3 years ago

I am sure $HOME exists, This is the full log, can reproduced every time! in my Mac Pro

➜  ~ ./test.kts
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100   707  100   707    0     0    309      0  0:00:02  0:00:02 --:--:--  4158
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11915  100 11915    0     0   7414      0  0:00:01  0:00:01 --:--:--  7414

                                -+syyyyyyys:
                            `/yho:`       -yd.
                         `/yh/`             +m.
                       .oho.                 hy                          .`
                     .sh/`                   :N`                `-/o`  `+dyyo:.
                   .yh:`                     `M-          `-/osysoym  :hs` `-+sys:      hhyssssssssy+
                 .sh:`                       `N:          ms/-``  yy.yh-      -hy.    `.N-````````+N.
               `od/`                         `N-       -/oM-      ddd+`     `sd:     hNNm        -N:
              :do`                           .M.       dMMM-     `ms.      /d+`     `NMMs       `do
            .yy-                             :N`    ```mMMM.      -      -hy.       /MMM:       yh
          `+d+`           `:/oo/`       `-/osyh/ossssssdNMM`           .sh:         yMMN`      /m.
         -dh-           :ymNMMMMy  `-/shmNm-`:N/-.``   `.sN            /N-         `NMMy      .m/
       `oNs`          -hysosmMMMMydmNmds+-.:ohm           :             sd`        :MMM/      yy
      .hN+           /d:    -MMMmhs/-.`   .MMMh   .ss+-                 `yy`       sMMN`     :N.
     :mN/           `N/     `o/-`         :MMMo   +MMMN-         .`      `ds       mMMh      do
    /NN/            `N+....--:/+oooosooo+:sMMM:   hMMMM:        `my       .m+     -MMM+     :N.
   /NMo              -+ooooo+/:-....`...:+hNMN.  `NMMMd`        .MM/       -m:    oMMN.     hs
  -NMd`                                    :mm   -MMMm- .s/     -MMm.       /m-   mMMd     -N.
 `mMM/                                      .-   /MMh. -dMo     -MMMy        od. .MMMs..---yh
 +MMM.                                           sNo`.sNMM+     :MMMM/        sh`+MMMNmNm+++-
 mMMM-                                           /--ohmMMM+     :MMMMm.       `hyymmmdddo
 MMMMh.                  ````                  `-+yy/`yMMM/     :MMMMMy       -sm:.``..-:-.`
 dMMMMmo-.``````..-:/osyhddddho.           `+shdh+.   hMMM:     :MmMMMM/   ./yy/` `:sys+/+sh/
 .dMMMMMMmdddddmmNMMMNNNNNMMMMMs           sNdo-      dMMM-  `-/yd/MMMMm-:sy+.   :hs-      /N`
  `/ymNNNNNNNmmdys+/::----/dMMm:          +m-         mMMM+ohmo/.` sMMMMdo-    .om:       `sh
     `.-----+/.`       `.-+hh/`         `od.          NMMNmds/     `mmy:`     +mMy      `:yy.
           /moyso+//+ossso:.           .yy`          `dy+:`         ..       :MMMN+---/oys:
         /+m:  `.-:::-`               /d+                                    +MMMMMMMNh:`
        +MN/                        -yh.                                     `+hddhy+.
       /MM+                       .sh:
      :NMo                      -sh/
     -NMs                    `/yy:
    .NMy                  `:sh+.
   `mMm`               ./yds-
  `dMMMmyo:-.````.-:oymNy:`
  +NMMMMMMMMMMMMMMMMms:`
    -+shmNMMMNmdy+:`

                                                                 Now attempting installation...

Looking for a previous installation of SDKMAN...
Looking for unzip...
Looking for zip...
Looking for curl...
Looking for sed...
Installing SDKMAN scripts...
Create distribution directories...
Getting available candidates...
Prime the config file...
Download script archive...
######################################################################## 100.0%-######################################################################## 100.0%
Extract script archive...
Install scripts...
Set version to 5.13.0 ...
Attempt update of login bash profile on OSX...
Attempt update of zsh profile...

All done!

Please open a new terminal, or run the following in the existing one:

    source "/Users/user/.sdkman/bin/sdkman-init.sh"

Then issue the following command:

    sdk help

Enjoy!!!
$ source /Users/user/.sdkman/bin/sdkman-init.sh
$ sdk install kscript
==== BROADCAST =================================================================
* 2021-10-21: springboot 2.5.6 available on SDKMAN!
* 2021-10-21: springboot 2.4.12 available on SDKMAN!
* 2021-10-20: gradle 7.3-rc-2 available on SDKMAN!
================================================================================

Downloading: kscript 3.1.0

In progress...

######################################################################## 100.0%-######################################################################## 100.0%

Installing: kscript 3.1.0
Done installing!

Setting kscript 3.1.0 as default.
$ source /Users/user/.sdkman/bin/sdkman-init.sh
./test.kts: line 6: exec: kscript: not found
➜  ~ $HOME
➜  ~ echo $HOME
/Users/user
➜  ~ kscript
zsh: command not found: kscript
➜  ~ source /Users/user/.sdkman/bin/sdkman-init.sh
➜  ~ kscript
kscript - Enhanced scripting support for Kotlin on *nix-based systems.

Usage:
 kscript [options] <script> [<script_args>]...
 kscript --clear-cache

The <script> can be a  script file (*kts), a script URL, - for stdin, a *.kt source file with a main method, or some kotlin code.

Use '--clear-cache' to wipe cached script jars and urls

Options:
 -i --interactive        Create interactive shell with dependencies as declared in script
 -t --text               Enable stdin support API for more streamlined text processing
 --idea                  Open script in temporary Intellij session
 -s --silent             Suppress status logging to stderr
 --package               Package script and dependencies into self-dependent binary
 --add-bootstrap-header  Prepend bash header that installs kscript if necessary

Copyright : 2020 Holger Brandl
License   : MIT
Version   : v3.1.0
Website   : https://github.com/holgerbrandl/kscript

I don't know why need execute source /Users/user/.sdkman/bin/sdkman-init.sh manually, I saw it has been executed by script😅

robert2411 commented 3 years ago

I tested it today on a clean MacBook install and there it doesn't work

acbc32778d1f:~ robertstevens$ ./test.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   707  100   707    0     0    859      0 --:--:-- --:--:-- --:--:--   859
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11915  100 11915    0     0  24209      0 --:--:-- --:--:-- --:--:-- 24466

                                -+syyyyyyys:
                            `/yho:`       -yd.
                         `/yh/`             +m.
                       .oho.                 hy                          .`
                     .sh/`                   :N`                `-/o`  `+dyyo:.
                   .yh:`                     `M-          `-/osysoym  :hs` `-+sys:      hhyssssssssy+
                 .sh:`                       `N:          ms/-``  yy.yh-      -hy.    `.N-````````+N.
               `od/`                         `N-       -/oM-      ddd+`     `sd:     hNNm        -N:
              :do`                           .M.       dMMM-     `ms.      /d+`     `NMMs       `do
            .yy-                             :N`    ```mMMM.      -      -hy.       /MMM:       yh
          `+d+`           `:/oo/`       `-/osyh/ossssssdNMM`           .sh:         yMMN`      /m.
         -dh-           :ymNMMMMy  `-/shmNm-`:N/-.``   `.sN            /N-         `NMMy      .m/
       `oNs`          -hysosmMMMMydmNmds+-.:ohm           :             sd`        :MMM/      yy
      .hN+           /d:    -MMMmhs/-.`   .MMMh   .ss+-                 `yy`       sMMN`     :N.
     :mN/           `N/     `o/-`         :MMMo   +MMMN-         .`      `ds       mMMh      do
    /NN/            `N+....--:/+oooosooo+:sMMM:   hMMMM:        `my       .m+     -MMM+     :N.
   /NMo              -+ooooo+/:-....`...:+hNMN.  `NMMMd`        .MM/       -m:    oMMN.     hs
  -NMd`                                    :mm   -MMMm- .s/     -MMm.       /m-   mMMd     -N.
 `mMM/                                      .-   /MMh. -dMo     -MMMy        od. .MMMs..---yh
 +MMM.                                           sNo`.sNMM+     :MMMM/        sh`+MMMNmNm+++-
 mMMM-                                           /--ohmMMM+     :MMMMm.       `hyymmmdddo
 MMMMh.                  ````                  `-+yy/`yMMM/     :MMMMMy       -sm:.``..-:-.`
 dMMMMmo-.``````..-:/osyhddddho.           `+shdh+.   hMMM:     :MmMMMM/   ./yy/` `:sys+/+sh/
 .dMMMMMMmdddddmmNMMMNNNNNMMMMMs           sNdo-      dMMM-  `-/yd/MMMMm-:sy+.   :hs-      /N`
  `/ymNNNNNNNmmdys+/::----/dMMm:          +m-         mMMM+ohmo/.` sMMMMdo-    .om:       `sh
     `.-----+/.`       `.-+hh/`         `od.          NMMNmds/     `mmy:`     +mMy      `:yy.
           /moyso+//+ossso:.           .yy`          `dy+:`         ..       :MMMN+---/oys:
         /+m:  `.-:::-`               /d+                                    +MMMMMMMNh:`
        +MN/                        -yh.                                     `+hddhy+.
       /MM+                       .sh:
      :NMo                      -sh/
     -NMs                    `/yy:
    .NMy                  `:sh+.
   `mMm`               ./yds-
  `dMMMmyo:-.````.-:oymNy:`
  +NMMMMMMMMMMMMMMMMms:`
    -+shmNMMMNmdy+:`

                                                                 Now attempting installation...

Looking for a previous installation of SDKMAN...
Looking for unzip...
Looking for zip...
Looking for curl...
Looking for sed...
Installing SDKMAN scripts...
Create distribution directories...
Getting available candidates...
Prime the config file...
Download script archive...
######################################################################## 100.0%-######################################################################## 100.0%
Extract script archive...
Install scripts...
Set version to 5.13.0 ...
Attempt update of login bash profile on OSX...
Added sdkman init snippet to /Users/robertstevens/.bash_profile
Attempt update of zsh profile...
Updated existing /Users/robertstevens/.zshrc

All done!

Please open a new terminal, or run the following in the existing one:

    source "/Users/robertstevens/.sdkman/bin/sdkman-init.sh"

Then issue the following command:

    sdk help

Enjoy!!!
$ source /Users/robertstevens/.sdkman/bin/sdkman-init.sh
$ sdk install kotlin
==== BROADCAST =================================================================
* 2021-10-26: gradle 7.3-rc-3 available on SDKMAN!
* 2021-10-21: springboot 2.5.6 available on SDKMAN!
* 2021-10-21: springboot 2.4.12 available on SDKMAN!
================================================================================

Downloading: kotlin 1.5.31

In progress...

######################################################################## 100.0%-######################################################################## 100.0%

Installing: kotlin 1.5.31
Done installing!

Setting kotlin 1.5.31 as default.
$ sdk install gradle

Downloading: gradle 7.2

In progress...

######################################################################## 100.0%

Installing: gradle 7.2
Done installing!

Setting gradle 7.2 as default.
$ sdk install kscript

Downloading: kscript 3.1.0

In progress...

######################################################################## 100.0%-######################################################################## 100.0%

Installing: kscript 3.1.0
Done installing!

Setting kscript 3.1.0 as default.
$ source /Users/robertstevens/.sdkman/bin/sdkman-init.sh
./test.sh: line 6: exec: kscript: not found
acbc32778d1f:~ robertstevens$ 

closing the terminal and rerun it gives:

acbc32778d1f:~ robertstevens$ ./test.sh 
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
holgerbrandl commented 3 years ago

It seems that the bootstrap header is not correct.

Currently with

#!/bin/bash

//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || curl -L "https://git.io/fpF1K" | bash 1>&2
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/

it can't find kscript because it has not sourced the init-script from SDK. I think source "$SDKMAN_DIR/bin/sdkman-init.sh" is missing in before exec. What do you think?

wuseal commented 3 years ago

I saw the log that I pasted, It has sourced the sdk script(auto executed by bootstrap script):

Setting kscript 3.1.0 as default.
$ source /Users/user/.sdkman/bin/sdkman-init.sh
./test.kts: line 6: exec: kscript: not found

But also error with can't find kscript

My bootstrap header is:

#!/bin/bash

//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || curl -L "https://git.io/fpF1K" | bash 1>&2
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/

//DEPS com.github.holgerbrandl:kutils:0.12
//DEPS org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2

It seems that there is no differences between us

And If I manually source the init script by myself, then it can find kscript

➜  ~ source /Users/user/.sdkman/bin/sdkman-init.sh
➜  ~ kscript
kscript - Enhanced scripting support for Kotlin on *nix-based systems.

Usage:
 kscript [options] <script> [<script_args>]...
 kscript --clear-cache

Very strange😅

wuseal commented 3 years ago

I fixed it by change the bootstrap header to next:

//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || curl -L "https://git.io/fpF1K" | bash 1>&2
source "$HOME/.sdkman/bin/sdkman-init.sh"
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/
wuseal commented 3 years ago

If I execute the test.kts by this way: . ./test.kts It seems also work without modify the bootstrap header

Ref: https://stackoverflow.com/questions/16011245/source-files-in-a-bash-script

test.kts' content:

//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || curl -L "https://git.io/fpF1K" | bash 1>&2
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/

println("Hello Kscript")

@holgerbrandl

holgerbrandl commented 3 years ago

However, it is still broken, as most users won't use/know this workaround. My bash skills are unfortunately also limited. We somehow need to backport the workaround into the preamble.

wuseal commented 3 years ago

I advice change the bootstrapheader to next:

//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || source /dev/stdin <<< "$(curl -L https://git.io/fpF1K)"
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/

Ref: https://stackoverflow.com/questions/10520605/bashs-source-command-not-working-with-a-file-curld-from-internet I'v test it, It works fine. How do you think @holgerbrandl

holgerbrandl commented 2 years ago

Thanks for the fix.