cantino / mcfly

Fly through your shell history. Great Scott!
MIT License
6.78k stars 177 forks source link

Fails to start on WSL #206

Open jmlucjav opened 2 years ago

jmlucjav commented 2 years ago

hi,

I just installed in WSL in windows 10, with the install script, and I get something very similar, anyone knows what's up?

error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help

thanks

Originally posted by @jmlucjav in https://github.com/cantino/mcfly/issues/128#issuecomment-962210659

cantino commented 2 years ago

When do you get this message?

jmlucjav commented 2 years ago

when I open a wsl terminal

cantino commented 2 years ago

Bash shell?

jmlucjav commented 2 years ago

yep

cantino commented 2 years ago

Hmm, I have no idea. There is no reference to is in mcfly.bash. What happens if you type mcfly --version or eval "$(mcfly init bash)" after the shell loads?

jmlucjav commented 2 years ago

I think I already tried and got the same output. But I'll confirm when I get to the pc where I installed mcfly, not there currently.

thanks!

jmlucjav commented 2 years ago

this is what I get when opening a bash terminal, and trying what you suggested:

error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
-Users/xx❯ mcfly --version
McFly 0.5.10
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
-Users/xx ➜ eval "$(mcfly init bash)"
-bash: return: can only `return' from a function or sourced script
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
cantino commented 2 years ago

Ok, I'm pretty baffled. It feels like maybe $? in WSL bash isn't an exit code?

You could try pasting this into a shell:

function mcfly_prompt_command {
  local exit_code=$? # Record exit status of previous command.

  echo "Exit code: '${exit_code}'"

  # Populate McFly's temporary, per-session history file from recent commands in the shell's primary HISTFILE.
  if [[ ! -f "${MCFLY_HISTORY}" ]]; then
    MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX)
    export MCFLY_HISTORY
    command tail -n100 "${HISTFILE}" >| "${MCFLY_HISTORY}"
  fi

  history -a "${MCFLY_HISTORY}" # Append history to $MCFLY_HISTORY.
  # Run mcfly with the saved code. It will:
  # * append commands to $HISTFILE, (~/.bash_history by default)
  #   for backwards compatibility and to load in new terminal sessions;
  # * find the text of the last command in $MCFLY_HISTORY and save it to the database.
  $MCFLY_PATH add --exit ${exit_code} --append-to-histfile
  # Clear the in-memory history and reload it from $MCFLY_HISTORY
  # (to remove instances of '#mcfly: ' from the local session history).
  history -cr "${MCFLY_HISTORY}"
  return ${exit_code} # Restore the original exit code by returning it.
}

PROMPT_COMMAND="mcfly_prompt_command"
ad-on-is commented 2 years ago

Arch on WSL2 works fine here.

jmlucjav commented 2 years ago

@cantino getting the same issue with that function. Does this shed any light?

~$  echo "look [$MCFLY_PATH]"
look [mcfly is /usr/local/bin/mcfly]
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~$
cantino commented 2 years ago

Yes! This is definitely the problem, which mcfly must return mcfly is /usr/local/bin/mcfly instead of just /usr/local/bin/mcfly in WSL. Could you tell me the output of the following commands?

jmlucjav commented 2 years ago
~ ❯ which mcfly
mcfly is /usr/local/bin/mcfly
error: Found argument 'is' which wasn't expected, or isn't valid in this
context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~ ➜ env which mcfly
/usr/local/bin/mcfly
error: Found argument 'is' which wasn't expected, or isn't valid in this
context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~ ➜ builtin which mcfly
-bash: builtin: which: not a shell builtin
error: Found argument 'is' which wasn't expected, or isn't valid in this
context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~ ❯ command which mcfly
/usr/local/bin/mcfly
error: Found argument 'is' which wasn't expected, or isn't valid in this
context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~ ➜

On Mon, Nov 15, 2021 at 7:31 PM Andrew Cantino @.***> wrote:

Yes! This is definitely the problem, which mcfly must return mcfly is /usr/local/bin/mcfly instead of just /usr/local/bin/mcfly in WSL. Could you tell me the output of the following commands?

  • which mcfly
  • env which mcfly
  • builtin which mcfly
  • command which mcfly

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cantino/mcfly/issues/206#issuecomment-969198064, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN2V5LWNPW23X4USCSENGLUMFGXRANCNFSM5HPX6VLQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

cantino commented 2 years ago

Thank you! How did you install mcfly? Is it easy for you to test a PR? If so, you could see if https://github.com/cantino/mcfly/pull/208 fixes it for you.

jmlucjav commented 2 years ago

hey sorry for the late relpy.

I installed like this:

curl -LSfs https://raw.githubusercontent.com/cantino/mcfly/master/ci/install.sh | sudo sh -s -- --git cantino/mcfly

notice I had to add sudo or something was failing, I don't recall what.

How do I go about testing your fix? Not sure...

cantino commented 2 years ago

I just updated master, you should be able to re-run that command to re-install. Please let me know if this fixes it for you @jmlucjav

jmlucjav commented 2 years ago

I installed master and I get exactly the same behaviour...I am not a bash connoiseur so maybe I screwed something on my side...

cantino commented 2 years ago

That's strange. What do you get if you type mcfly init bash?

jmlucjav commented 2 years ago
~ ➜ mcfly init bash
#!/bin/bash

# Ensure stdin is a tty
[[ -t 0 ]] || return

# Avoid loading this file more than once
if [[ "$__MCFLY_LOADED" == "loaded" ]]; then
  return 0
fi
__MCFLY_LOADED="loaded"

# Ensure HISTFILE exists.
export HISTFILE="${HISTFILE:-$HOME/.bash_history}"
if [[ ! -r "${HISTFILE}" ]]; then
  echo "McFly: ${HISTFILE} does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly."
  return 1
fi

# MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session.
MCFLY_SESSION_ID="$(command dd if=/dev/urandom bs=256 count=1 2> /dev/null | LC_ALL=C command tr -dc 'a-zA-Z0-9' | command head -c 24)"
export MCFLY_SESSION_ID

# Find the binary
MCFLY_PATH=${MCFLY_PATH:-$(which mcfly)}
if [ -z "$MCFLY_PATH" ]; then
  echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.bash."
  return 1
fi

# Ignore commands with a leading space
export HISTCONTROL="${HISTCONTROL:-ignorespace}"

# Append new history items to .bash_history
shopt -s histappend

# Setup a function to be used by $PROMPT_COMMAND.
function mcfly_prompt_command {
  local exit_code=$? # Record exit status of previous command.

  # Populate McFly's temporary, per-session history file from recent commands in the shell's primary HISTFILE.
  if [[ ! -f "${MCFLY_HISTORY}" ]]; then
    MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX)
    export MCFLY_HISTORY
    command tail -n100 "${HISTFILE}" >| "${MCFLY_HISTORY}"
  fi

  history -a "${MCFLY_HISTORY}" # Append history to $MCFLY_HISTORY.
  # Run mcfly with the saved code. It will:
  # * append commands to $HISTFILE, (~/.bash_history by default)
  #   for backwards compatibility and to load in new terminal sessions;
  # * find the text of the last command in $MCFLY_HISTORY and save it to the database.
  $MCFLY_PATH add --exit ${exit_code} --append-to-histfile
  # Clear the in-memory history and reload it from $MCFLY_HISTORY
  # (to remove instances of '#mcfly: ' from the local session history).
  history -cr "${MCFLY_HISTORY}"
  return ${exit_code} # Restore the original exit code by returning it.
}

# Set $PROMPT_COMMAND run mcfly_prompt_command, preserving any existing $PROMPT_COMMAND.
if [ -z "$PROMPT_COMMAND" ]
then
  PROMPT_COMMAND="mcfly_prompt_command"
else
  PROMPT_COMMAND="mcfly_prompt_command;${PROMPT_COMMAND#;}"
fi

# If this is an interactive shell, take ownership of ctrl-r.
if [[ $- =~ .*i.* ]]; then
  if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
    # shellcheck disable=SC2016
    bind -x '"\C-r": "echo \#mcfly: ${READLINE_LINE[@]} >> $MCFLY_HISTORY ; READLINE_LINE= ; mcfly search"'
  else
    # The logic here is:
    #   1. Jump to the beginning of the edit buffer, add 'mcfly: ', and comment out the current line. We comment out the line
    #      to ensure that all possible special characters, including backticks, are ignored. This commented out line will
    #      end up as the most recent entry in the $MCFLY_HISTORY file.
    #   2. Type "mcfly search" and then run the command. McFly will pull the last line from the $MCFLY_HISTORY file,
    #      which should be the commented-out search from step #1. It will then remove that line from the history file and
    #      render the search UI pre-filled with it.
    if set -o | grep "vi " | grep -q on; then
      bind "'\C-r': '\e0i#mcfly: \e\C-m mcfly search\C-m'"
    else
      bind "'\C-r': '\C-amcfly: \e# mcfly search\C-m'"
    fi
  fi
fi
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
cantino commented 2 years ago

MCFLY_PATH=${MCFLY_PATH:-$(which mcfly)}, this is the old version of mcfly. Try

curl -LSfs https://raw.githubusercontent.com/cantino/mcfly/master/ci/install.sh | sudo sh -s -- --git cantino/mcfly -f

The -f at the end should cause it to force install. Or uninstall the current copy of mcfly (located at which mcfly).

jmlucjav commented 2 years ago
~ ❯ sudo rm /usr/local/bin/mcfly
[sudo] password for xx:
-bash: /usr/local/bin/mcfly: No such file or directory
~ ➜ which mcfly
-bash: type: mcfly: not found
-bash: /usr/local/bin/mcfly: No such file or directory
~ ❯ curl -LSfs https://raw.githubusercontent.com/cantino/mcfly/master/ci/install.sh | sudo sh -s -- --git cantino/mcfly -f
install.sh: GitHub repository: https://github.com/cantino/mcfly
install.sh: Crate: mcfly
install.sh: Tag: latest (v0.5.10)
install.sh: Target: x86_64-unknown-linux-musl
install.sh: Downloading: https://github.com/cantino/mcfly/releases/download/v0.5.10/mcfly-v0.5.10-x86_64-unknown-linux-musl.tar.gz
install.sh: Installing to: /usr/local/bin
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~ ➜ which mcfly
mcfly is /usr/local/bin/mcfly
error: Found argument 'is' which wasn't expected, or isn't valid in this context

USAGE:
    mcfly [FLAGS] [OPTIONS] <SUBCOMMAND>

For more information try --help
~ ➜
cantino commented 2 years ago

Still erroring after restarting your shell?

jmlucjav commented 2 years ago

yep....

On Tue, Nov 30, 2021 at 10:52 PM Andrew Cantino @.***> wrote:

Still erroring after restarting your shell?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cantino/mcfly/issues/206#issuecomment-983052974, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN2V5L7FSJE5F6XN36DBYLUOVBTDANCNFSM5HPX6VLQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

jmlucjav commented 2 years ago

FYI, I have installed this on a new laptop, latest WSL with ubuntu20.04 and I get the same.

The issue is closed by the way.

cantino commented 2 years ago

Okay, I see the problem. I'll make a new release.

cantino commented 2 years ago

Okay, I released 0.5.11, would you mind updating?

jmlucjav commented 2 years ago

the issue is gone :)

In one of the computers I got this, not sure if it's related:

McFly: Upgrading McFly DB to version 3, please wait...thread 'main' panicked at 'McFly error: Unable to add cmd_tpl to commands (duplicate column name: cmd_tpl)', src/history/schema.rs:41:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
~ ➜
cantino commented 2 years ago

If you don't care about the old mcfly history on that computer, you could delete ~/.mcfly/history.db on that computer.

On Sun, Dec 12, 2021 at 1:04 PM jmlucjav @.***> wrote:

the issue is gone :)

In one of the computers I got this, not sure if it's related:

McFly: Upgrading McFly DB to version 3, please wait...thread 'main' panicked at 'McFly error: Unable to add cmd_tpl to commands (duplicate column name: cmd_tpl)', src/history/schema.rs:41:17

note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

~ ➜

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/cantino/mcfly/issues/206#issuecomment-991970000, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAUO66FNBWK7UKBEBX7AYDUQUE6VANCNFSM5HPX6VLQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

jmlucjav commented 2 years ago

removed it, but the error keeps showing up...on every command I run

cantino commented 2 years ago

What happens if you do export MCFLY_DEBUG=1 and then use the shell?

jmlucjav commented 2 years ago

I enabled these two:

export MCFLY_DEBUG=1
export RUST_BACKTRACE=1

and I get:

McFly: Upgrading McFly DB to version 3, please wait...thread 'main' panicked at 'McFly error: Unable to add cmd_tpl to commands (duplicate column name: cmd_tpl)', src/history/schema.rs:41:17
stack backtrace:
   0: rust_begin_unwind
             at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/std/src/panicking.rs:517:5
   1: std::panicking::begin_panic_fmt
             at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/std/src/panicking.rs:460:5
   2: mcfly::history::schema::migrate::{{closure}}
   3: mcfly::history::schema::migrate
   4: mcfly::history::history::History::load
   5: mcfly::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
cantino commented 2 years ago

I assume it keeps happening after a rm -rf ~/.mcfly too?

jmlucjav commented 2 years ago

indeed...

cantino commented 2 years ago

Unfortunately, I don't know know what's going on. I don't use WSL. Maybe someone who does can work on debugging this.

stefanegg commented 2 years ago

I had the same issue, but I could resolve it using the steps below. Maybe this helps.

  1. Remove/comment out the eval "$(mcfly init bash)" line in .bashrc
  2. Close/reopen the shell to make sure McFly is not loaded
  3. Delete the command history using rm ~/.bash_history
  4. Delete McFly settings directory using rm -Rf ~/.mcfly
  5. Close/reopen the shell and check if you have a new ~/.bash_history file (the file only gets updated when closing the shell, and McFly panics when it can't find the file)
  6. Add/uncomment the eval "$(mcfly init bash)" line in .bashrc (from step 1)
jmlucjav commented 2 years ago

I had the same issue, but I could resolve it using the steps below. Maybe this helps.

  1. Remove/comment out the eval "$(mcfly init bash)" line in .bashrc
  2. Close/reopen the shell to make sure McFly is not loaded
  3. Delete the command history using rm ~/.bash_history
  4. Delete McFly settings directory using rm -Rf ~/.mcfly
  5. Close/reopen the shell and check if you have a new ~/.bash_history file (the file only gets updated when closing the shell, and McFly panics when it can't find the file)
  6. Add/uncomment the eval "$(mcfly init bash)" line in .bashrc (from step 1)

it did work, thanks!