Skip to content

Conversation

@tjzurlin
Copy link
Collaborator

@tjzurlin tjzurlin commented Dec 8, 2025

Bug Fix: tmp model does not recompile when .model is edited locally

Summary

When using a local .model file with the loadModel(writeTemp = TRUE), the compiled model in tmp/ does not recompile when the local .model file is edited.

Root Cause Analysis

A file.copy is used to copy the local .model file to the tmp/ directory. The hash key is then created on the copied tmp model. If the local .model file is edited, the hash is not changed in tmp/ and the tmp model is not recompiled.

Todo:

  • Handle path normalization consistently across windows and linux
  • Use intermediate source_file for hash comparison and model compilation

Information

Two files are now tracked: source_file and file.

  1. source_file: Original file. This is the .model file in the user's local directory or the tmp file first written from mString. The hash for file changes is determined from this file.
  2. file: This is the file that is compiled by mod.

@tjzurlin
Copy link
Collaborator Author

tjzurlin commented Jan 18, 2026

MCSimMod Package Updates

Overview

Fixed critical bug in writeTemp=TRUE functionality that made it so that local models compiled to a temp directory were not recompiled if local model file was changed. This fix delineates between source_file (the model file being edited) and model_file (the model file that is compiled). This fix also implements proper hash tracking for source files and improved package testing infrastructure.

Key Bug Fix: Hash Calculation for writeTemp=TRUE

Problem:
When writeTemp=TRUE, the package would not recognize changes to the local model file.

Solution:
Improved Package Initialization Logic:

  • initialize(): Sets up file structure and creates initial hash (no compilation)
  • Utilize winslash="/" in normalizePath to standardize windows paths and remove need for grep in .fixPath()
  • Add source_file (edited by user) vs model_file (compiled by 'mod') to better track files

Modified compileModel() method

  • Added source_file parameter with default source_file = model_file
  • Changed hash calculation to use source_file instead of model_file
  • Maintains backward compatibility for writeTemp = FALSE cases
  • Added recompiled attribute to track if model has been recompiled following change in source_file

Updated loadModel() method:

  • Pass source_file = paths$source_file to compileModel()
  • Ensure model file is updated from source before compilation when writeTemp = TRUE

Added Test

New test for writeTemp=TRUE functionality:

  • Tests proper source/compilation file separation

  • Validates complete recompilation lifecycle:

    • First load: compilation occurs
    • Second load: no unnecessary recompilation
    • Source edit: recompilation triggered
    • Fourth load: no infinite recompilation (bug fix verification)
  • Ensures hash tracking works correctly

@tjzurlin tjzurlin marked this pull request as ready for review January 18, 2026 21:16
@tjzurlin tjzurlin requested review from Copilot and dfkapraun January 18, 2026 21:16
Copy link
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

This PR fixes a bug where models using loadModel(writeTemp = TRUE) would not recompile when the local .model file was edited. The fix introduces separate tracking of the source file (where users make edits) and the compilation file (in the temp directory), ensuring the hash is calculated from the source file to detect changes correctly.

Changes:

  • Separated source file and compilation file tracking in the Model class
  • Updated hash calculation to use the source file instead of the temp compilation file
  • Added comprehensive test coverage for the writeTemp functionality

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
R/MCSim_model.R Implements source_file/file separation, updates hash initialization and loadModel to track source changes
R/compileModel.R Adds source_file parameter for hash calculation, replaces seq(length()) with seq_along()
R/fileHasChanged.R Updates to use source_file parameter and tools::md5sum
R/fixPath.R Removes Windows-specific path normalization code
tests/testthat/test-compareHash.R Adds comprehensive test for writeTemp functionality
tests/testthat/test-sim.R Adds showWarnings = FALSE to dir.create call
man/compileModel.Rd Documents new source_file parameter
man/Model-class.Rd Documents new recompiled field
DESCRIPTION Updates RoxygenNote version and License format
.Rbuildignore Adds paper directory to ignore list

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

tjzurlin and others added 3 commits January 18, 2026 16:20
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Collaborator

@dfkapraun dfkapraun left a comment

Choose a reason for hiding this comment

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

Looks good! See comment for "MCSim_model.R" and let me know what you think.

@dfkapraun dfkapraun self-assigned this Jan 21, 2026
Copy link
Collaborator

@dfkapraun dfkapraun left a comment

Choose a reason for hiding this comment

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

Looks good! Approving...

@dfkapraun dfkapraun merged commit e571e8f into main Jan 21, 2026
3 checks passed
@dfkapraun dfkapraun deleted the bugfix/write-tmp-model branch January 21, 2026 19:47
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.

3 participants