code: func (s *LocalState) WriteState(state *terraform.State) error {
  	s.mu.Lock()
  	defer s.mu.Unlock()
  
  	if s.stateFileOut == nil {
  		if err := s.createStateFiles(); err != nil {
  			return nil
  		}
  	}
  	defer s.stateFileOut.Sync()
  
  	s.state = state.DeepCopy() // don't want mutations before we actually get this written to disk
  
  	if s.readState != nil && s.state != nil {
  		// We don't trust callers to properly manage serials. Instead, we assume
  		// that a WriteState is always for the next version after what was
  		// most recently read.
  		s.state.Serial = s.readState.Serial
  	}
  
  	if _, err := s.stateFileOut.Seek(0, io.SeekStart); err != nil {
  		return err
  	}
  	if err := s.stateFileOut.Truncate(0); err != nil {
  		return err
  	}
  
  	if state == nil {
  		// if we have no state, don't write anything else.
  		return nil
  	}
  
  	if !s.state.MarshalEqual(s.readState) {
  		s.state.Serial++
  	}
  
  	if err := terraform.WriteState(s.state, s.stateFileOut); err != nil {
  		return err
  	}
  
  	s.written = true
  	return nil
  }
docs: // WriteState for LocalState always persists the state as well.
  // TODO: this should use a more robust method of writing state, by first
  // writing to a temp file on the same filesystem, and renaming the file over
  // the original.
  //
  // StateWriter impl.