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

Errors with taskfile and windows #1723

Closed
JCHacking opened this issue Jul 15, 2024 · 5 comments
Closed

Errors with taskfile and windows #1723

JCHacking opened this issue Jul 15, 2024 · 5 comments

Comments

@JCHacking
Copy link

I have a Taskfile which I want to be compatible with both windows and linux, for this I have done the following:

task:
  cmds:
      - cmd: |
          AUTHOR_NAME=""
          AUTHOR_EMAIL=""
          if [ -n "${AUTHOR_NAME}" ] && [ -n "${AUTHOR_EMAIL}" ]; then
            export GIT_COMMIT_AUTHOR="${AUTHOR_NAME} <${AUTHOR_EMAIL}>"
          fi

          semantic-release --strict version
          semantic-release --strict publish
        platforms: [ linux ]
      - cmd: |
          (
            for /f "tokens=*" %%a in ("git config user.name") do (set AUTHOR_NAME=%%a)
            for /f "tokens=*" %%a in ("git config user.email") do (set AUTHOR_EMAIL=%%a)

            if "%AUTHOR_NAME%" neq "" if "%AUTHOR_EMAIL%" neq "" (
              set GIT_COMMIT_AUTHOR="%AUTHOR_NAME% <%AUTHOR_EMAIL%>"
            )

            semantic-release --strict version
            semantic-release --strict publish
          )
        platforms: [ windows ]

Whether writing the windows part in powershell or in batch I run into sh validations with errors like "for foo" must be followed by "in", "do", ;, or a newline

Is there any way to avoid these pre-validations for windows systems? Unfortunately I can't use Linux, WSL or git bash and I would like a more solid alternative to use commands in windows.

  • Task version: 3.37.2
  • Operating system: Windows
  • Experiments enabled: False
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Jul 15, 2024
@JCHacking JCHacking changed the title Errors with taskfile/sh and windows Errors with taskfile and windows Jul 15, 2024
@pd93
Copy link
Member

pd93 commented Jul 17, 2024

Taskfile does not directly invoke bash/powershell on your system. Instead it uses a cross-platform shell written in Go called gosh. The syntax of this shell is POSIX/bash compliant, so you should use this syntax regardless as to whether you're on Windows or Linux.

One problem you might encounter when trying to write cross-platform tasks is that gosh does not include some coreutils (e.g. cp, rm, ls etc.) that you might be used to using on unix-like systems (#197, mvdan/sh#93) and so the commands available can vary between systems.

If you really want to use Powershell on Windows, you can invoke it manually at the start of your command. e.g.

my-task:
  cmds:
    - "powershell <my_powershell_cmd>"

@pd93 pd93 closed this as completed Jul 17, 2024
@pd93 pd93 added type: question Further information is requested. and removed state: needs triage Waiting to be triaged by a maintainer. labels Jul 17, 2024
@JCHacking
Copy link
Author

JCHacking commented Jul 17, 2024

Taskfile does not directly invoke bash/powershell on your system. Instead it uses a cross-platform shell written in Go called gosh. The syntax of this shell is POSIX/bash compliant, so you should use this syntax regardless as to whether you're on Windows or Linux.

One problem you might encounter when trying to write cross-platform tasks is that gosh does not include some coreutils (e.g. cp, rm, ls etc.) that you might be used to using on unix-like systems (#197, mvdan/sh#93) and so the commands available can vary between systems.

If you really want to use Powershell on Windows, you can invoke it manually at the start of your command. e.g.

my-task:
  cmds:
    - "powershell <my_powershell_cmd>"

Although what you say is true, my problem is not that the commands such as rm, cp, etc. are different.

My problem is that I can not put a small script as in the example
Is there any way to launch the command directly? Why to make so many previous validations? Why not give an option to keep it simple by simply substituting variables and launching the command?

It seems that even though this is the compiled for windows, the restriction that cmd has to be in POSIX/bash breaks the concept of multiplatform.

I usually develop in Python, and the libreries I've collaborated with to execute something do this

  • Get default shell
  • Launch command like this:
    SHELL -c COMMAND
    For example
    bash -c echo Test
    (Here you have to make a small adaptation to make it work with cmd since instead of -c it is /c)
  • Redirect the standard output and the error output to stdout and stderr (This is how the execution is seen in the terminal).
  • Validate

@pd93
Copy link
Member

pd93 commented Jul 17, 2024

It is not a restriction. You are able to run Powershell if you want to. It just needs to be invoked as mentioned. However, the whole point of using gosh is that you don't need to write separate scripts for Windows/Linux in the majority of cases. It is multi-platform because the shell works on both platforms rather than requiring you to write the same task twice in two different shell languages.

Have you tried just removing the platform: linux/windows and running the task you have for Linux on Windows? I see no reason why it shouldn't work.

@JCHacking
Copy link
Author

JCHacking commented Jul 17, 2024

Ouu you are absolutely right, I didn't think of taskfile this way. This approach is a marvel!

I have seen a problem but it is with the semantic relese implementation for python, I will do a PR to fix it but it is nothing that matters here for this issue.

The only drawback I see with this approach is the lack of applications, since writing in bash something you want to run on windows will surely fail for lack of commands like awk, grep, cp, etc.

@pd93
Copy link
Member

pd93 commented Jul 17, 2024

The only drawback I see with this approach is the lack of applications, since writing in bash something you want to run on windows will surely fail for lack of commands like awk, grep, cp, etc.

Agreed. The absence of some coreutils/builtins on some platforms is a known restriction and something we plan to address in the future (#197, mvdan/sh#93, mvdan/sh#97).

In the meantime, it is possible to work around some of these issues by using the {{OS}} command in templates or the platforms keyword.

@pd93 pd93 removed the type: question Further information is requested. label Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants