Skip to content

fix: respect pre-set environment variables in build template#6394

Open
Bahtya wants to merge 3 commits intoflet-dev:mainfrom
Bahtya:fix/env-vars-priority
Open

fix: respect pre-set environment variables in build template#6394
Bahtya wants to merge 3 commits intoflet-dev:mainfrom
Bahtya:fix/env-vars-priority

Conversation

@Bahtya
Copy link
Copy Markdown

@Bahtya Bahtya commented Apr 8, 2026

Problem

The generated main.dart in the build template unconditionally overwrites all FLET_* environment variables. This makes it impossible to customize them in containerized or sandboxed environments (e.g., Flatpak on Linux), where the app should respect pre-set system environment variables.

Root Cause

  1. environmentVariables is initialized as an empty map {} instead of inheriting from Platform.environment
  2. All FLET_* values are set via direct assignment (map["KEY"] = value) which always overwrites

Fix

  1. Initialize environmentVariables from Platform.environment so the map starts with all existing system env vars
  2. Use putIfAbsent() instead of direct assignment for all FLET_* variables, so pre-set values are preserved while defaults are applied when unset

Before:

Map<String, String> environmentVariables = {};
// ...
environmentVariables["FLET_SERVER_PORT"] = tcpPort.toString(); // always overwrites

After:

Map<String, String> environmentVariables = Map.from(Platform.environment);
// ...
environmentVariables.putIfAbsent("FLET_SERVER_PORT", () => tcpPort.toString()); // respects existing

Fixes #6393

Summary by Sourcery

Preserve and extend environment variables when running the generated Dart main entrypoint so that existing FLET_* settings from the host environment are respected.

Bug Fixes:

  • Initialize the Dart build template's environment variable map from the host process environment instead of starting empty, ensuring system-provided variables are retained.
  • Apply FLET_* environment variable defaults only when they are not already set, avoiding overwriting pre-configured values in containerized or sandboxed environments.

The generated main.dart unconditionally overwrites FLET_* environment
variables (FLET_SERVER_PORT, FLET_SERVER_UDS_PATH, FLET_APP_STORAGE_DATA,
etc.), making it impossible to customize them in containerized or sandboxed
environments (e.g., Flatpak on Linux).

Fix by:
1. Initializing environmentVariables from Platform.environment instead of
   an empty map
2. Using putIfAbsent() instead of direct assignment so pre-set values are
   preserved

Fixes flet-dev#6393

Signed-off-by: bahtya <bahtyar153@qq.com>
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

We've reviewed this pull request using the Sourcery rules engine

Copy link
Copy Markdown
Contributor

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

Note

Copilot was unable to run its full agentic suite in this review.

Updates the Dart build template to preserve host-provided FLET_* environment variables by inheriting the current process environment and only applying defaults when keys are unset (fixes #6393).

Changes:

  • Initialize environmentVariables from Platform.environment instead of an empty map.
  • Replace direct environmentVariables["KEY"] = ... assignments with putIfAbsent(...) for all FLET_* defaults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if (!kIsWeb && assetsDir.isNotEmpty) {
environmentVariables["FLET_ASSETS_DIR"] = assetsDir;
environmentVariables.putIfAbsent("FLET_ASSETS_DIR", () => assetsDir);
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

Line 212 is indented less than the surrounding block, which makes the control flow harder to read and look inconsistent with the rest of the file. Re-indent the statement to align with the contents of the if block (and consider running dart format on the file to keep formatting consistent).

Suggested change
environmentVariables.putIfAbsent("FLET_ASSETS_DIR", () => assetsDir);
environmentVariables.putIfAbsent("FLET_ASSETS_DIR", () => assetsDir);

Copilot uses AI. Check for mistakes.
Bahtya added 2 commits April 8, 2026 23:32
Signed-off-by: bahtya <bahtyar153@qq.com>
Signed-off-by: bahtya <bahtyar153@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Flet build template ignores ENV VARs

3 participants