Problems using GetEnvironmentVariable lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 09:51 Hello, I am trying to use the SysUtils function GetEnvironmentVariable in an LCL GUI Application, but the function always returns #0. Yet, if I use the function in a simple console application, it returns the correct value. Can anyone help? ---------------------------------------------------------------------- JuhaManninen · 2017-06-18 10:02 Quote from: carl_caulkett on June 18, 2017, 11:51:13 amCan anyone help? Not without example code. ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 10:16 Okay... This works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program envvar_console; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes,  SysUtils  { you can add units after this }; var  v: string;begin  v := GetEnvironmentVariable('GOPATH');  WriteLn(v);end.           and this doesn't: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);  private  public  { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  S: string;begin  S := SysUtils.GetEnvironmentVariable('GOPATH');  Label1.Caption := S;end; end.                                     The project options are set to default in both cases. Thanks in advance for any help. ---------------------------------------------------------------------- JuhaManninen · 2017-06-18 10:29 The code looks correct. It means the actual environment is different. Maybe it depends on how you start the application. From console / desktop etc. You did not mention your OS but I assume Windows. ---------------------------------------------------------------------- jwdietrich · 2017-06-18 10:33 Quote from: JuhaManninen on June 18, 2017, 12:29:37 pmThe code looks correct. It means the actual environment is different. Maybe it depends on how you start the application. From console / desktop etc. You did not mention your OS but I assume Windows. The question is in the subforum "Mac OS X", therefore I think it refers to macOS. ---------------------------------------------------------------------- jwdietrich · 2017-06-18 10:35 Quote from: carl_caulkett on June 18, 2017, 11:51:13 amHello, I am trying to use the SysUtils function GetEnvironmentVariable in an LCL GUI Application, but the function always returns #0. Yet, if I use the function in a simple console application, it returns the correct value. Can anyone help? Are you sure that the GOPATH environment variable is globally set? If I use your code with other environment variables, e. g. USER, it works correctly, even in an LCL app. ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 10:36 I'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus. ---------------------------------------------------------------------- jwdietrich · 2017-06-18 10:37 Quote from: carl_caulkett on June 18, 2017, 12:36:30 pmI'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus. Did you try the code with the USER environment variable? ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 10:38 Quote from: jwdietrich on June 18, 2017, 12:33:18 pmQuote from: JuhaManninen on June 18, 2017, 12:29:37 pmThe code looks correct. It means the actual environment is different. Maybe it depends on how you start the application. From console / desktop etc. You did not mention your OS but I assume Windows. The question is in the subforum "Mac OS X", therefore I think it refers to macOS. Well spotted ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 10:45 Quote from: jwdietrich on June 18, 2017, 12:37:17 pmQuote from: carl_caulkett on June 18, 2017, 12:36:30 pmI'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus. Did you try the code with the USER environment variable? Hmmm... I've just tried with the USER environment variable and that works in both cases. There must be something odd about the GOPATH variable. ---------------------------------------------------------------------- JuhaManninen · 2017-06-18 10:47 Quote from: jwdietrich on June 18, 2017, 12:33:18 pmThe question is in the subforum "Mac OS X", therefore I think it refers to macOS. Ooops, sorry. My bad. I don't know much about macOS. ---------------------------------------------------------------------- jwdietrich · 2017-06-18 10:53 Just for fun, I have made a mini project that shows the contents of arbitrary environment variables as read by the LCL. It works well on macOS (and probably any other platform). See attachment. Unlike your code, it uses the function GetEnvironmentVariableUTF8 from the LazUTF8 unit. You have to add LazUTF8 in the uses section of your code. ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 11:02 Quote from: carl_caulkett on June 18, 2017, 12:45:18 pmQuote from: jwdietrich on June 18, 2017, 12:37:17 pmQuote from: carl_caulkett on June 18, 2017, 12:36:30 pmI'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus. Did you try the code with the USER environment variable? Hmmm... I've just tried with the USER environment variable and that works in both cases. There must be something odd about the GOPATH variable. The GOPATH environment variable is defined in the fish.config file for the Fish shell that I am using for Golang development. It is defined with the following line: set -x GOPATH $HOME/Code/go This all happens at the shell level. It seems odd that the variable is available in the console app but not the GUI app. I would have thought it would be an "All or nothing" type situation. Presumably, the GetEnvironmentVariable function is behaving differently in the different environments. The situation is made more difficult by the problems inherent in debugging in Lazarus on the Apple Mac, although I know that Xcode can be used - it's just a bit fiddly! ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 11:12 Quote from: jwdietrich on June 18, 2017, 12:53:44 pmJust for fun, I have made a mini project that shows the contents of arbitrary environment variables as read by the LCL. It works well on macOS (and probably any other platform). See attachment. Unlike your code, it uses the function GetEnvironmentVariableUTF8 from the LazUTF8 unit. You have to add LazUTF8 in the uses section of your code. I've tried your project and while it works for environment variables such as USER and PATH, it doesn't work for GOPATH. Presumably, the GOPATH variable is not a "Proper" environment variable, though what does actually constitute a "Proper" environment variable is beyond me. I may have to risk humiliation and consult the almighty beings of StackOverflow in order to find out, though I was hoping to keep things within the warm and cosy confines of the Lazarus forums! ---------------------------------------------------------------------- jwdietrich · 2017-06-18 11:28 The difference may result from the fact that you launch CLI applications made with FPC from the shell, but GUI applications based on the LCL from the Finder. For permanent storage of Environment variables, macOS uses two approaches: Environment variables for use by the shell and CLI programs are stored in ~/.bash_profile (you may want to type more ~/.bash_profile to display the current contents of this file on your computer), while environment variables for GUI applications are stored in ~/.MacOSX/environment.plist (for Mac OS X Lion and Mountain Lion only) or in /etc/launchd.conf (for newer versions of macOS). Additionally, the syntax is slightly different: In ~/.bash_profile the environment variables are set with Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i export PATH=$PATH:/Library/PostgreSQL/8.4/bin but with Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i setenv JRUBY_HOME /Applications/Dev/jruby in /etc/launchd.conf. ---------------------------------------------------------------------- carl_caulkett · 2017-06-18 12:56 Quote from: jwdietrich on June 18, 2017, 01:28:42 pmThe difference may result from the fact that you launch CLI applications made with FPC from the shell, but GUI applications based on the LCL from the Finder. For permanent storage of Environment variables, macOS uses two approaches: Environment variables for use by the shell and CLI programs are stored in ~/.bash_profile (you may want to type more ~/.bash_profile to display the current contents of this file on your computer), while environment variables for GUI applications are stored in ~/.MacOSX/environment.plist (for Mac OS X Lion and Mountain Lion only) or in /etc/launchd.conf (for newer versions of macOS). Additionally, the syntax is slightly different: In ~/.bash_profile the environment variables are set with Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i export PATH=$PATH:/Library/PostgreSQL/8.4/bin but with Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i setenv JRUBY_HOME /Applications/Dev/jruby in /etc/launchd.conf. I suspect the difference may be the fact that I'm using Fish as my shell as opposed to Bash. The Fish method of setting environment variables doesn't appear to be sufficient to affect GUI apps. I made the changes that you suggested to /etc/launchd.conf but that didn't seem to make any difference. I may have done something wrong here, of course!