Skip to content

Add environment path management for installation#4514

Open
eduardomozart wants to merge 6 commits intotesseract-ocr:mainfrom
eduardomozart:patch-3
Open

Add environment path management for installation#4514
eduardomozart wants to merge 6 commits intotesseract-ocr:mainfrom
eduardomozart:patch-3

Conversation

@eduardomozart
Copy link
Copy Markdown
Contributor

Description

This PR enhances the Windows installer by automatically adding the Tesseract installation directory to the system or user PATH environment variable. This allows users and automated scripts to run tesseract.exe from any command prompt immediately after installation without manual configuration.

Motivation

Standardizing the PATH configuration during installation improves the out-of-the-box experience and simplifies automated, unattended infrastructure deployments. Previously, users had to manually edit their environment variables after installing.

Key Changes

  • Dynamic Privilege Handling: The script now checks both $MultiUser.InstallMode and the actual account privileges (UserInfo::GetAccountType). It safely writes to HKLM if running as an Administrator (All Users) or falls back to HKCU for standard users (Current User).
  • Safe Environment Variable Manipulation: Integrated the EnVar plugin to modify the PATH. This is highly recommended over native NSIS registry commands to avoid truncating existing PATH strings that exceed 1024 characters.
  • Clean Uninstallation: Added logic to the uninstaller to dynamically check the installation mode and safely remove the Tesseract directory from the PATH.

Notes for Reviewers

⚠️ Build Dependency Update: Compiling tesseract.nsi now requires the EnVar plugin to be present in the NSIS plugins directory.

@stweil
Copy link
Copy Markdown
Member

stweil commented Feb 19, 2026

Due to limitations of the possible PATH length for Windows and undefined behaviour when the PATH length reaches the limit, this feature is not desired.

Personally I also don't like lengthy parts which affect all running software and can produce unexpected side effects. It would also make parallel installations of different Tesseract versions more error prone.

@eduardomozart
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I completely understand your concerns regarding the Windows PATH length limitations and how global state changes can cause unexpected side effects for users running parallel installations for testing.

To address this while still supporting automated infrastructure deployments, I have updated the PR to make this feature strictly opt-in.

Here are the adjustments made in the latest commit:

  • Unchecked by default (Opt-in): The "Add to PATH" feature is now a visible component in the UI, but it is unchecked by default. Standard users will not have their environment variables modified unless they explicitly select it. image
  • Command-line trigger for silent installs: I added a new /addenvpath command-line parameter. System administrators can pass this flag (e.g., setup.exe /S /addenvpath) to automatically enable the PATH registration during unattended/headless deployments. (Note: This flag is case-insensitive).
  • Safe string handling: The implementation still relies on the EnVar plugin to safely read and write to the registry, ensuring we bypass the native NSIS 1024-character truncation limit and do not corrupt existing long PATH strings.

This approach ensures that researchers keep their isolated environments safe by default, while still giving system administrators a standard way to deploy Tesseract globally across multiple endpoints.

@stweil
Copy link
Copy Markdown
Member

stweil commented Feb 20, 2026

Setting PATH was removed more than 10 years ago in commit c886e3b because it resulted in an empty PATH (!) if the old PATH was very long. This problem was reported in issue #481.

@eduardomozart, can you confirm that we won't re-introduce this old problem with your pull request?

@eduardomozart
Copy link
Copy Markdown
Contributor Author

I can definitively confirm that this PR will not re-introduce the issue from #481.

The bug from 10 years ago was caused by native NSIS registry commands (ReadRegStr / WriteRegStr), which had a hardcoded string limit of 1024 characters. If a system's PATH exceeded that limit, the native commands would truncate or completely empty the string when writing it back.

By using the EnVar plugin, we bypass this native NSIS limitation entirely. EnVar uses the Windows API directly to safely read and append to environment variables regardless of their length.

To be absolutely certain, I tested this locally. I intentionally bloated my PATH environment variable to 1094 characters (far exceeding the old NSIS limit):

C:\Program Files\ImageMagick-7.1.2-Q16-HDRI;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Users\eduardo.oliveira\AppData\Local\Programs\Python\Python311;C:\Users\eduardo.oliveira\AppData\Local\Programs\Python\Python311\scripts;C:\Program Files\Microsoft VS Code\bin;C:\Program Files\PuTTY\;C:\Program Files (x86)\NSIS;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Users\eduardo.oliveira\AppData\Local\Programs\Streamlink\bin;C:\Users\eduardo.oliveira\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\eduardo.oliveira\AppData\Local\Programs\Python\Python311\;C:\Users\eduardo.oliveira\AppData\Local\Microsoft\WindowsApps;C:\Users\eduardo.oliveira\AppData\Local\GitHubDesktop\bin;C:\Users\eduardo.oliveira\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-8.0.1-full_build\bin;

The installer handled it perfectly and appended C:\Program Files (x86)\Tesseract-OCR to the PATH variable without breaking, truncating, or emptying the existing string.

@stweil
Copy link
Copy Markdown
Member

stweil commented Feb 21, 2026

It looks like the new UI texts with translations are still missing.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds automatic PATH environment variable management to the Tesseract Windows installer, allowing users to run tesseract.exe from any command prompt without manual configuration. The implementation adds an optional installer section with privilege-aware logic to modify either system (HKLM) or user (HKCU) PATH, and includes corresponding cleanup in the uninstaller.

Changes:

  • Added optional "Add to PATH environment variable" installer section with privilege checking
  • Implemented automatic PATH removal during uninstallation
  • Added command-line flag /ADDENVPATH for automated/silent installations

Comment thread nsis/tesseract.nsi
Comment thread nsis/tesseract.nsi Outdated
Comment thread nsis/tesseract.nsi Outdated
Comment thread nsis/tesseract.nsi Outdated
Comment thread nsis/tesseract.nsi
stweil and others added 3 commits February 21, 2026 14:51
…ion)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Added functionality to manage environment PATH for Tesseract installation.
@eduardomozart
Copy link
Copy Markdown
Contributor Author

Hello @stweil,
All Copilot suggestions have been addressed. Regarding the missing translation strings, the English string has been committed to #4516 (my bad), but the Portuguese string has been added.

@amitdo amitdo added the nsis label Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants