Skip to content
129 changes: 107 additions & 22 deletions scripts/setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,56 @@ if (-not (Test-Command cmake)) {
Write-Host "CMake is already installed. Version: $(cmake --version)" -ForegroundColor Green
}

# --------------------------------------------------
# Miniconda
# --------------------------------------------------
$condaRoot = "$env:USERPROFILE\miniconda3"
$condaExe = "$condaRoot\Scripts\conda.exe"

if (-not (Test-Path $condaExe)) {
Write-Host "Installing Miniconda..." -ForegroundColor Yellow
$installer = "$env:TEMP\miniconda.exe"
Invoke-WebRequest `
-Uri "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" `
-OutFile $installer

if ($LASTEXITCODE -ne 0 -or !(Test-Path $installer)) {
throw "Failed to download Miniconda installer"
}
Comment on lines +128 to +130
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

$LASTEXITCODE is not set by Invoke-WebRequest; this check is misleading.

Invoke-WebRequest is a PowerShell cmdlet — it throws on failure rather than setting $LASTEXITCODE. The exit-code half of this condition is inert and could mask failures if a prior native command left $LASTEXITCODE non-zero. Rely on Test-Path alone, or wrap the download in its own try/catch.

Proposed fix
-    if ($LASTEXITCODE -ne 0 -or !(Test-Path $installer)) {
+    if (!(Test-Path $installer)) {
         throw "Failed to download Miniconda installer"
     }
🤖 Prompt for AI Agents
In `@scripts/setup.ps1` around lines 128 - 130, The conditional checking
$LASTEXITCODE is incorrect because Invoke-WebRequest throws on failure rather
than setting $LASTEXITCODE; update the download failure handling in the block
that references Invoke-WebRequest, $installer and the throw "Failed to download
Miniconda installer" to either (a) remove the `$LASTEXITCODE -ne 0` check and
rely solely on `Test-Path $installer` before throwing, or (b) wrap the
Invoke-WebRequest call in a try/catch that catches the exception and throws a
new error including the caught exception message so the throw includes real
failure details.


Start-Process -Wait -FilePath $installer -ArgumentList `
"/InstallationType=JustMe",
"/AddToPath=0",
"/RegisterPython=0",
"/S",
"/D=$condaRoot"

if ($LASTEXITCODE -ne 0) {
throw "Miniconda installer exited with an error"
}
Comment on lines +132 to +136
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Installer arguments passed as a single string — silent install will likely fail.

$installerArgs is a single string, so & $installer $installerArgs passes it as one argument to the NSIS installer. The individual switches (/S, /D=…, etc.) won't be parsed. Use Start-Process with -ArgumentList (which accepts a single string and passes it correctly to the process command line) or invoke the executable with discrete arguments.

Proposed fix using Start-Process
-    $installerArgs = "/InstallationType=JustMe /AddToPath=0 /RegisterPython=0 /S /D=$condaRoot"
-    & $installer $installerArgs
-    if ($LASTEXITCODE -ne 0) {
-        throw "Miniconda installer failed with exit code $LASTEXITCODE"
-    }
+    $process = Start-Process -Wait -PassThru -FilePath $installer -ArgumentList `
+        "/InstallationType=JustMe", "/AddToPath=0", "/RegisterPython=0", "/S", "/D=$condaRoot"
+    if ($process.ExitCode -ne 0) {
+        throw "Miniconda installer failed with exit code $($process.ExitCode)"
+    }
🤖 Prompt for AI Agents
In `@scripts/setup.ps1` around lines 132 - 136, The current invocation uses a
single string in $installerArgs and calls & $installer $installerArgs which
passes the entire string as one argument; replace this with Start-Process to
pass the argument string correctly and capture the true exit code: call
Start-Process -FilePath $installer -ArgumentList $installerArgs -Wait
-NoNewWindow -PassThru and then check the returned process object's ExitCode
(e.g., $proc = Start-Process ... -PassThru; if ($proc.ExitCode -ne 0) { throw
... }) instead of relying on $LASTEXITCODE; alternatively, build an argument
array and invoke the installer with the splat operator (& $installer `@args`) to
pass discrete switches.


Remove-Item $installer

# Validate installation
if (-not (Test-Path $condaExe)) {
throw "Conda executable not found after Miniconda installation"
}

Write-Host "Miniconda installed successfully." -ForegroundColor Green
}
else {
Write-Host "Miniconda already installed. Skipping installation." -ForegroundColor Green
}

# --------------------------------------------------
# Initialize Conda (THIS WAS MISSING BEFORE)
# --------------------------------------------------
$env:Path = "$condaRoot;$condaRoot\Scripts;$condaRoot\condabin;$env:Path"

$condaBase = & $condaExe info --base
& "$condaBase\shell\condabin\conda-hook.ps1"


# ---- Set up the frontend ----
Write-Host "Setting up frontend..." -ForegroundColor Yellow
try {
Expand All @@ -131,19 +181,37 @@ try {
# ---- Set up the backend using Python 3.12 ----
Write-Host "Setting up backend..." -ForegroundColor Yellow
try {
Set-Location .\backend\

# Create virtual environment
python -m venv .env

# Activate virtual environment and install dependencies
.\.env\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
deactivate


Set-Location .\backend\

Write-Host "Setting up backend Conda environment..." -ForegroundColor Cyan

# Remove existing environment (ignore if it does not exist)
conda remove -n backend_env --all -y
if ($LASTEXITCODE -ne 0) {
Write-Host "Environment did not exist or could not be removed, continuing..." -ForegroundColor Yellow
}

# Create fresh environment
conda create -n backend_env python=3.12 -y
Copy link
Contributor

Choose a reason for hiding this comment

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

Command should match the manual setup guide

The environment names and flags should be the same.

Copy link
Author

Choose a reason for hiding this comment

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

Ok

if ($LASTEXITCODE -ne 0) {
throw "Failed to create Conda environment"
}

# Upgrade pip inside the environment
conda run -n backend_env python -m pip install --upgrade pip
if ($LASTEXITCODE -ne 0) {
throw "Failed to upgrade pip inside Conda environment"
}

# Install backend dependencies
conda run -n backend_env python -m pip install -r requirements.txt
if ($LASTEXITCODE -ne 0) {
throw "Failed to install backend dependencies"
}

Set-Location ..

Write-Host "Backend setup completed successfully." -ForegroundColor Green
} catch {
Write-Host "Error setting up backend: $_" -ForegroundColor Red
Expand All @@ -155,17 +223,34 @@ Write-Host "Setting up sync-microservice..." -ForegroundColor Yellow
try {
Set-Location .\sync-microservice\

# Create virtual environment
python -m venv .sync-env

# Activate virtual environment and install dependencies
.\.sync-env\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
deactivate

Write-Host "Setting up sync-microservice Conda environment..." -ForegroundColor Cyan

# Remove existing environment (ignore if it does not exist)
conda remove -n sync_env --all -y
if ($LASTEXITCODE -ne 0) {
Write-Host "Environment did not exist or could not be removed, continuing..." -ForegroundColor Yellow
}

# Create fresh environment
conda create -n sync_env python=3.12 -y
if ($LASTEXITCODE -ne 0) {
throw "Failed to create Conda environment for sync-microservice"
}

# Upgrade pip inside the environment
conda run -n sync_env python -m pip install --upgrade pip
Copy link
Contributor

Choose a reason for hiding this comment

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

Activate conda and then directly use pip install. Again, these commands should match the manual setup guide.

if ($LASTEXITCODE -ne 0) {
throw "Failed to upgrade pip in sync_env"
}

# Install dependencies inside the environment
conda run -n sync_env python -m pip install -r requirements.txt
if ($LASTEXITCODE -ne 0) {
throw "Failed to install sync-microservice dependencies"
}
Comment on lines 233 to 251
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

conda install -r requirements.txt is invalid — -r is pip syntax, not conda.

Line 248 uses conda install -r requirements.txt, but conda install does not support the -r flag for reading a requirements file. This will fail at runtime. It should be pip install -r requirements.txt, consistent with the backend block (line 205).

Also, the same -y and conda activate issues flagged in the backend block apply here (lines 234, 239).

Proposed fix (sync-microservice block)
-    conda create -p .sync-env python=3.12 
+    conda create -p .sync-env python=3.12 -y
     if ($LASTEXITCODE -ne 0) {
         throw "Failed to create Conda environment for sync-microservice"
     }
 
-    conda activate .\.sync-env
-
-    # Upgrade pip inside the environment
-    python -m pip install --upgrade pip
+    conda run -p .sync-env python -m pip install --upgrade pip
     if ($LASTEXITCODE -ne 0) {
         throw "Failed to upgrade pip in sync_env"
     }
 
-    # Install dependencies inside the environment
-    conda install -r requirements.txt
+    conda run -p .sync-env pip install -r requirements.txt
     if ($LASTEXITCODE -ne 0) {
         throw "Failed to install sync-microservice dependencies"
     }
-    # Deactivate environment after setup
-    conda deactivate
     Set-Location ..
🤖 Prompt for AI Agents
In `@scripts/setup.ps1` around lines 233 - 251, The script uses invalid conda
flags and unsafe activation: replace the failing "conda install -r
requirements.txt" with "pip install -r requirements.txt" (run inside the created
env after activating), add the non-interactive flag when creating the env (use
conda create with -y for "conda create -p .sync-env python=3.12 -y" or
equivalent), and avoid a plain "conda activate .\.sync-env" that may fail in
non-interactive shells—use the recommended env activation approach for scripts
(e.g., source/conda run or the conda hook) so subsequent commands like "python
-m pip install --upgrade pip" and "pip install -r requirements.txt" run
reliably; update the commands around conda create, conda activate, and the pip
install steps to follow this sequence.


Set-Location ..

Write-Host "Sync-microservice setup completed successfully." -ForegroundColor Green
} catch {
Write-Host "Error setting up sync-microservice: $_" -ForegroundColor Red
Expand Down