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

NNNLVL increments on every shell invocation (macOS) #639

Closed
psevdaisthisi opened this issue Jun 6, 2020 · 8 comments
Closed

NNNLVL increments on every shell invocation (macOS) #639

psevdaisthisi opened this issue Jun 6, 2020 · 8 comments
Labels

Comments

@psevdaisthisi
Copy link

[x] Operating System: macOS Mojave 10.14.6
[x] Terminal Emulator: iTerm2 (v3.3.10)
[x] Shell: either fish (v3.1.2) or bash (v5.0.17)
[x] Issue exists on nnn master (reproduced 65cf067)

Exact steps to reproduce the issue

Same as in #510. Works fine on my Arch box (LTS kernel v5.4.44, Fish v3.1.2), but on macOS NNLVL still increases as reported in that issue.

I was able fix it by doing the following change:

diff --git a/src/nnn.c b/src/nnn.c
index e822a4c..e46508e 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -6381,7 +6381,7 @@ nochange:
 
 				setenv(envs[ENV_NCUR], (ndents ? dents[cur].name : ""), 1);
 				spawn(shell, NULL, NULL, F_CLI);
-				setenv(env_cfg[NNNLVL], xitoa(tmp ? atoi(tmp) : 0), 1);
+				setenv(env_cfg[NNNLVL], xitoa(tmp ? atoi(tmp) - 1: 0), 1);
 				r = TRUE;
 				break;
 			case SEL_LAUNCH:

But in Arch it will wraparound. I could put it in a __APPLE__ guard but doesn't seem a good idea because I still don't know what the underlying problem is.

Thank you for any attention you could give to this.

@jarun
Copy link
Owner

jarun commented Jun 6, 2020

Can you please print tmp in debug mode after:

tmp = getenv(env_cfg[NNNLVL]);

https://github.com/jarun/nnn/wiki/Developer-guides#debugging-nnn

@psevdaisthisi
Copy link
Author

With this change:

diff --git a/src/nnn.c b/src/nnn.c
index e822a4c..514d7eb 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -6377,6 +6377,7 @@ nochange:
 			case SEL_SHELL:
 				/* Set nnn nesting level */
 				tmp = getenv(env_cfg[NNNLVL]);
+				DPRINTF_S(tmp);
 				setenv(env_cfg[NNNLVL], xitoa((tmp ? atoi(tmp) : 0) + 1), 1);
 
 				setenv(envs[ENV_NCUR], (ndents ? dents[cur].name : ""), 1);

I got the following log by starting nnn ./nnn and repeating 3 times the process of spawning a shell with ! and quitting it. Please don't take offence on the Junk directory. It's my default directory to put anything I need to work with right way.

ln 7081: VERSION=3.2
ln 7106: home=/Users/dangerware
ln 6810: xdgcfg=/Users/dangerware/.config
ln 6852: cfgdir=/Users/dangerware/.config/nnn
ln 6857: plugindir=/Users/dangerware/.config/nnn/plugins
ln 6867: sessiondir=/Users/dangerware/.config/nnn/sessions
ln 6891: selpath=/Users/dangerware/.config/nnn/.selection
ln 7113: opener=/usr/bin/open
ln 7194: getenv(envs[ENV_VISUAL])=(null)
ln 7195: getenv(envs[ENV_EDITOR])=nvim
ln 7196: editor=nvim
ln 7200: pager=less
ln 7204: shell=/usr/local/bin/fish
ln 7206: getenv("PWD")=/Users/dangerware/Junk/nnn
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=201000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=(null)
ln 1645: status=0
ln 1717: pid=8851
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=206000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=0
ln 1645: status=0
ln 1717: pid=8865
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=195000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=1
ln 1645: status=0
ln 1717: pid=8879
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=305000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=2
ln 1645: status=0
ln 1717: pid=8893
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=187000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn

@jarun
Copy link
Owner

jarun commented Jun 6, 2020

Can you print tmp after:

setenv(env_cfg[NNNLVL], xitoa((tmp ? atoi(tmp) : 0) + 1), 1);

and

setenv(env_cfg[NNNLVL], xitoa(tmp ? atoi(tmp) : 0), 1);

Let's figure where tmp is changing.

@psevdaisthisi
Copy link
Author

With these changes:

diff --git a/src/nnn.c b/src/nnn.c
index e822a4c..9e22f57 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -6377,11 +6377,14 @@ nochange:
 			case SEL_SHELL:
 				/* Set nnn nesting level */
 				tmp = getenv(env_cfg[NNNLVL]);
+				DPRINTF_S(tmp);
 				setenv(env_cfg[NNNLVL], xitoa((tmp ? atoi(tmp) : 0) + 1), 1);
+				DPRINTF_S(tmp);
 
 				setenv(envs[ENV_NCUR], (ndents ? dents[cur].name : ""), 1);
 				spawn(shell, NULL, NULL, F_CLI);
 				setenv(env_cfg[NNNLVL], xitoa(tmp ? atoi(tmp) : 0), 1);
+				DPRINTF_S(tmp);
 				r = TRUE;
 				break;
 			case SEL_LAUNCH:

I get the following log:

ln 7083: VERSION=3.2
ln 7108: home=/Users/dangerware
ln 6812: xdgcfg=/Users/dangerware/.config
ln 6854: cfgdir=/Users/dangerware/.config/nnn
ln 6859: plugindir=/Users/dangerware/.config/nnn/plugins
ln 6869: sessiondir=/Users/dangerware/.config/nnn/sessions
ln 6893: selpath=/Users/dangerware/.config/nnn/.selection
ln 7115: opener=/usr/bin/open
ln 7196: getenv(envs[ENV_VISUAL])=(null)
ln 7197: getenv(envs[ENV_EDITOR])=nvim
ln 7198: editor=nvim
ln 7202: pager=less
ln 7206: shell=/usr/local/bin/fish
ln 7208: getenv("PWD")=/Users/dangerware/Junk/nnn
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=134000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=(null)
ln 6382: tmp=(null)
ln 1645: status=0
ln 1717: pid=2351
ln 6387: tmp=(null)
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=127000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=0
ln 6382: tmp=1
ln 1645: status=0
ln 1717: pid=2365
ln 6387: tmp=1
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=128000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=1
ln 6382: tmp=2
ln 1645: status=0
ln 1717: pid=2379
ln 6387: tmp=2
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=121000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn
ln 6380: tmp=2
ln 6382: tmp=3
ln 1645: status=0
ln 1717: pid=2393
ln 6387: tmp=3
ln 4492: __FUNCTION__=dentfill
ln 4729: ts2.tv_nsec - ts1.tv_nsec=120000
ln 5152: __FUNCTION__=redraw
ln 5167: path=/Users/dangerware/Junk/nnn

The first pass seems fine. We get null in all evaluations because at the time of the first getenv that environment variable was not set. But then on further passes the value is changed after the first setenv. I don't understand why this isn't also happening in the first pass.

I can imagine that the memory for which getenv returns a pointer can be changed automatically by setenv, but then the behaviour should be the same after NNNLVL transitions for null to 0.

@jarun
Copy link
Owner

jarun commented Jun 6, 2020

Can you try an unsetenv() before the first setenv()?

@jarun jarun closed this as completed in fdfbac0 Jun 6, 2020
@jarun
Copy link
Owner

jarun commented Jun 6, 2020

Please confirm if the following patch works.

@psevdaisthisi
Copy link
Author

@jarun It works! I've tested it on both macOS and Arch Linux. Thank you!

@jarun
Copy link
Owner

jarun commented Jun 7, 2020

Awesome!

@jarun jarun mentioned this issue Jun 7, 2020
7 tasks
@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants