Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[BUG] If the result of a command is a single newline, assigning it to a variable results in an empty string

Open lens0021 opened this issue 1 year ago • 2 comments

To Reproduce A short version: amber eval 'let source = "\n"; source = trust $ echo "\$\{{nameof source}}" $; trust $ echo \$\{{nameof source}@Q} $''' ($'\n' is expected)

Longer:

fun print_h(text) {
  trust $ echo \$\{{nameof text}@Q} $
}

let source = "\n"
// original
print_h(source)
// assign
source = trust $ echo "\$\{{nameof source}}" $
// should be the same as the original
print_h(source)

Unexpectedly, the source is replaced with an empty string.

$'\n'
''

Expected behavior

$'\n'
$'\n'

Additional context

  1. Here is the transpiled result of a short version:
echo 'let source = "\n"; source = trust $ echo "\$\{{nameof source}}" $; trust $ echo \$\{{nameof source}@Q} $' | amber build - -
#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.4.0-alpha
# date: 2024-12-24 22:10:02
__0_source="
"
__AMBER_VAL_0=$(echo "${__0_source}")
__AS=$?
__0_source="${__AMBER_VAL_0}"
echo ${__0_source@Q}
__AS=$?

if __AMBER_VAL_0=$(echo "${__0_source}") is replaced with __AMBER_VAL_0="$(echo \"${__0_source}\")", the problem disappears.

  1. This is not related to IFS.

lens0021 avatar Dec 24 '24 13:12 lens0021

is this what you mean?

let test = trust $echo$;
assert_eq(test, "\n"); // fails

b1ek avatar Jan 18 '25 09:01 b1ek

Yes, that is shorter. 👍

lens0021 avatar Jan 18 '25 12:01 lens0021