VS Code with PlatformIO - ESP8266 EEPROM.WRITE appears to execute before older instructions

I’ve written a simple routine to detect when a new version of my code is running by comparing a declared version (const String pgmVersion = "00.09";) to a version saved in EEPROM (which is actually flash in the ESP8266-12E).

I have a first for loop that steps through characters in pgmVersion and the saved version to see if they match. There is a second loop that is only executed if there is a mismatch between the declared and saved version identification. The first loop uses EEPROM.read and the second loop uses EEPROM.write.

Herein lies the problem, when the declared version is known to be different than the saved version, the code still reports via the first loop that they are the same and the write loop does not get executed. Never-the-less, the EEPROM.write function does get executed as the new program version is found there even after a power up.

It appears that the EEPROM.write function is being hoisted up somewhere in my code by the compilation process. This does NOT happen when I run the same code under the Arduino IDE. It only happens when running under VS Code with PlatformIO.

I opened an issue in Stack Overflow before I understood that this was an issue with VS Code with PlatformIO. This link has the code code and my findings: ESP8266 EEPROM READ/WRITE - Write seems to happen before read of old value

    #include <Arduino.h>
    #include <EEPROM.h>
    const String pgmVersion = "00.04";
    void setup() {
      Serial.begin(115200);
      EEPROM.begin(6);
      delay(500);
      char eepData;
      char pgmData;
      bool pgmMatch = true;
      for (unsigned int i = 0; i < pgmVersion.length(); i++) 
      {
        eepData = char(EEPROM.read(i));
        pgmData = pgmVersion.charAt(i);
        Serial.print("eepData = ");
        Serial.println(eepData);
        Serial.print("pgmVersion[i] = ");
        Serial.println(pgmData);
        
        if (eepData == pgmData)
        {
          Serial.println("eepData matches pgmData at index " + String(i));
        } else
        {
          Serial.println("eepData does NOT match pgmData at index " + String(i));
          pgmMatch = false;
        }
      }
        if (!pgmMatch)
        {
          Serial.println("Writing EEPROM");
          for (unsigned int i = 0; i < pgmVersion.length(); i++) 
          {
            pgmData = pgmVersion.charAt(i);
            EEPROM.write(i,pgmData);
            delay(10);
          }
          if (EEPROM.commit()) 
          {
            Serial.println("EEPROM successfully committed");
          } else 
          {
            Serial.println("ERROR! EEPROM commit failed");
          }
        }
    }

    void loop() {
      // put your main code here, to run repeatedly:
    }

So it looks like the VS Code version with PlatformIO reorders the code by hoisting the eeprom write somewhere or something with that effect. If that is actually the case, what is needed is a fix to some piece of platform code or some sort of barrier instruction to prevent this from happening. Or something else?

To complete the example: What platformio.ini do you have?

The platformio.ini has the following:

; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; Redirecting...

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200